Last active
August 29, 2015 14:05
-
-
Save maxplomer/08c28dfa6ebfa7b571c3 to your computer and use it in GitHub Desktop.
find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Integer | |
def isabundant? | |
sum_of_proper_divisors(self) > self | |
end | |
end | |
def sum_of_proper_divisors(num) #borrowed from github user mlsimpson | |
orig = num | |
if num == 1 | |
return 0 | |
end | |
sum = 1 | |
p = 2 | |
while p*p <= num && num > 1 | |
if (num%p).zero? | |
j = p*p | |
num = (num / p) | |
while (num%p).zero? | |
j = j*p | |
num = (num / p) | |
end | |
sum = sum*(j-1) | |
sum = sum / (p-1) | |
else | |
end | |
if p == 2 | |
p = 3 | |
else | |
p = p+2 | |
end | |
end | |
if num > 1 | |
sum = sum * (num+1) | |
end | |
sum - orig | |
end | |
def timing_method(block=nil, *args) #borrowed from github user mlsimpson | |
start = Time.now | |
if block_given? | |
yield | |
else | |
self.send(block, args) | |
end | |
finish = Time.now | |
print "Elapsed time: #{(finish - start)*1000} milliseconds\n" | |
end | |
def euler23 | |
maxnum = 20161 #Wolfram Mathworld's discussion on Abundant Numbers, "Every number greater than 20161 can be expressed as a sum of two abundant numbers. " So our upper bound is 20161 instead of 28123 | |
allnums = (1..maxnum).to_a | |
sumarray = [] | |
abarray = allnums.select{|i| i if i.isabundant? } | |
for i in 0..abarray.length-1 | |
for j in i..abarray.length-1 | |
sum = abarray[i] + abarray[j] | |
sumarray << sum unless sum > maxnum | |
end | |
end | |
sumarray = sumarray.uniq | |
(allnums - sumarray).inject(:+) | |
end | |
timing_method do | |
puts euler23 | |
end | |
# Problem Statement: | |
#perfect number: sum of proper divisors equal to number | |
# | |
#sum of proper divisors of 28 is 1+2+4+7+14=28, 28 is a perfect number | |
# | |
#n is deficient is s.o.p.d. is less than n, abundant if more | |
# | |
#12 smallest abundant number, 1+2+3+4+6=16 | |
# | |
#smallest number that can be written as the sum of two abundant numbers is 24 (12+12) | |
# | |
#all integers > 28123 can be written as the sum of two abundant numbers | |
# | |
#the greatest number than cannot be expressed as the sum of two abundant numbers is less than this limit | |
# | |
#find the sum of all the positive integers which cannot be written as the sum of two abundant numbers | |
# | |
# | |
# | |
#Algorithm Discussion: | |
# This is a very simple and slow solution that borrow a fast isabundant? function from a different solution | |
# | |
# First we generate all abundant numbers between 1 and maxnum, then we create a sumarray of all the possible sums that can be created from it, then we take the unique of that and remove that array from the all numbers array and take the sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment