# Find all prime factors of a number. def prime_factors(n) r = [] u = 101 i = 2 while i*i <= n c = 0 while n % i == 0 n /= i c += 1 end if c != 0 r << [i, c] end # 1224..24 # (Idk if this is any faster than just incrementing by one) d, u = (u * 10).divmod(825) i += d end r << [n, 1] if n > 1 r end # Test. 10.times do |n| p prime_factors(n) end p prime_factors(2**31-1) p prime_factors(2**31) p prime_factors(3**3*5**5) p prime_factors(2*3*5*7*11*13*17*19) p prime_factors(70) # Problem that prompted this.
Standard input is empty
[] [] [[2, 1]] [[3, 1]] [[2, 2]] [[5, 1]] [[2, 1], [3, 1]] [[7, 1]] [[2, 3]] [[3, 2]] [[2147483647, 1]] [[2, 31]] [[3, 3], [5, 5]] [[2, 1], [3, 1], [5, 1], [7, 1], [11, 1], [13, 1], [17, 1], [19, 1]] [[2, 1], [5, 1], [7, 1]]