My try to solve Project Euler Problem 21 using Ruby
def d(num)
num_array = Array.new
# this num/2 is used to cut the time for this execution into half
for i in 1..num/2
if num % i == 0 #&& i != num
num_array << i
end
end
total_num = 0
total_num = num_array.inject(:+).to_i
return total_num
end
def cmp(num,num_c)
if d(num) == num_c && num != num_c
return true
else
return false
end
end
total_sum = 0
for i in 1..10000
if cmp(d(i), i) == true
puts i
total_sum += i
end
end
puts total_sum
No comments:
Post a Comment