The radical of n, rad(n), is the product of distinct prime factors of n. For example, 504 = 2
3
3
2
7, so rad(504) = 2
3
7 = 42.
If we calculate rad(n) for 1
n
10, then sort them on rad(n), and sorting on n if the radical values are equal, we get:
Unsorted |
|
Sorted |
n |
rad(n) |

|
n |
rad(n) |
k |
1 | 1 |
|
1 | 1 | 1 |
2 | 2 |
|
2 | 2 | 2 |
3 | 3 |
|
4 | 2 | 3 |
4 | 2 |
|
8 | 2 | 4 |
5 | 5 |
|
3 | 3 | 5 |
6 | 6 |
|
9 | 3 | 6 |
7 | 7 |
|
5 | 5 | 7 |
8 | 2 |
|
6 | 6 | 8 |
9 | 3 |
|
7 | 7 | 9 |
10 | 10 |
|
10 | 10 | 10 |
Let E(k) be the kth element in the sorted n column; for example, E(4) = 8 and E(6) = 9.
If rad(n) is sorted for 1
n
100000, find E(10000).
Ruby: Running time = 15.94s
+-#PrimeList
class PrimeList
@@primes=[2,3,5,7,11,13,17,19]
def initialize
@next=-1
end
def last
return 0 if @next==-1
PrimeList.get(@next)
end
def getNext
PrimeList.get(@next+=1)
end
def [](i)
PrimeList.get(i)
end
def PrimeList.isPrime?(n)
if(@@primes[-1] >= n)
return true if @@primes.index n
return false
end
p=PrimeList.new
sq=Math.sqrt(n)
while(p.last<=sq)
return false if n % (p.getNext)==0
end
true
end
def PrimeList.get(i)
PrimeList.gen while @@primes.length<=i
@@primes[i]
end
def PrimeList.getPrimeNear(n)
PrimeList.gen until n<@@primes[-1]
@@primes[PrimeList.primeIndexNear(n)]
end
private
def PrimeList.primeIndexNear(n)
return 0 if n<@@primes[0]
return [-1] if n>@@primes[-1]
PrimeList.getIndexNear(@@primes,n)
end
def PrimeList.getIndexNear(list,n)
return 0 if list.length==1
if(list.length==2)
avg=(list[0]+list[1])/2.0
return 1 if n>avg
return 0
end
splitAfter=list.size/2
return PrimeList.getIndexNear(list[0..splitAfter],n) if n<list[splitAfter]
return splitAfter+1+PrimeList.getIndexNear(list[(splitAfter+1)..-1],n) if n>list[splitAfter+1]
avg=(list[splitAfter]+list[splitAfter+1])/2
return splitAfter+1 if n>avg
splitAfter
end
def PrimeList.gen
add=@@primes[-1]
cands=Set.new(1..(5*add)){|i|2*i+add}
theBegin=add+2
theEnd=11*add
highest=Math.sqrt(theEnd).to_i
upTo=PrimeList.primeIndexNear(highest)
upTo-=1 if @@primes[upTo]>highest
@@primes[1..upTo].each do |mult|
((theBegin-theBegin%mult)..(theEnd-theEnd%mult + mult)).step(mult){|i|cands.delete i}
end
@@primes=@@primes+cands.to_a.sort
end
end
+-#factors
def factors(n)
return [] if n<2
p=PrimeList.new
sq=Math.sqrt(n)
while(p.last<=sq)
if n % (p.getNext)==0
return [p.last]+factors(n/p.last)
end
end
return [n]
end
+-#Enumerable
module Enumerable
def sum
self.inject{|u,v|u+v}
end
def product
self.inject{|u,v|u*v}
end
def count(ob)
self.select{|i|i==ob}.length
end
def take_while
return [] unless yield(self.first)
if(self.class==Range)
evalPoint=self.first
evalPoint=evalPoint.succ while yield(evalPoint.succ) and not evalPoint==self.last
return self.first..evalPoint
else
s=self.to_a
upTo=1
upTo+=1 while yield(s[upTo]) and upTo<self.length
return self[0...upTo]
end
end
end
def p124
puts (1..100000).map{|i|[(factors(i).uniq.product or 1),i]}.sort[9999].last
end