challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

commit bac5074cbfc0f15dc0b96c28ae3aed2201b49d48
parent 136e1ee9e8b217fc930b3eea53a70e01ddb07fb0
Author: Ryan Wolf <rwolf@borderstylo.com>
Date:   Sat, 12 Feb 2011 13:02:08 -0800

soln for 23

Diffstat:
A023/023.rb | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+), 0 deletions(-)

diff --git a/023/023.rb b/023/023.rb @@ -0,0 +1,51 @@ +#!/usr/bin/ruby + +def abundant? (n) + sum = 1 + r = Math.sqrt(n) + for d in (2...r) + if n % d == 0 + sum += d + (n / d) + if sum > n + return true + end + end + end + if n % r == 0 + sum += r + end + sum > n +end + +as = [] +as_hash = Hash.new + +for i in (12..28123) + if abundant? i + as.push i + as_hash[i] = 1 + end +end + +# cannot be written as the sum of two abundant numbers +def cbwatsotabn? (n, as, as_hash) + half = n / 2 + for a in as + diff = n - a + if diff < half + return true + end + if as_hash[diff] + return false + end + end + return true +end + +sum = 0 +for n in (1...28123) + if cbwatsotabn? n, as, as_hash + sum += n + end +end +puts sum