Here is my solution. Its not the most beautiful thing in the world, but
its effective.
def weirdo_exhaustive(from, max)
puts “none found” and return 0 if max < 70
(from…max).each do |num|
if num % 2 == 0
list, sum= [], 0
(1…num).each do |div|
list << div and sum += div if num % div == 0
end
puts “==” + num.to_s if sum > num and has_sum(num, list) == false
end
end
end
def has_sum(num, array)
if array.is_a? Integer then return false end
sum = 0
array.each do |val| sum += val end
if sum === num then return true end #this next line saves a BUNCH of checks.
if sum < num then return false end
array.each do |removeme|
copy = array.dup
copy.delete(removeme)
if copy.size > 1 and has_sum(num, copy) == true then return true
end
end
return false
end
It took a good number of optimizations that make it less pretty, but
much faster. The first time I ran it it took 4 hours to reach 1,000.
Its faster than that now, but its still somewhere around O(3^n).
SO… I thought of a way to speed up the algorithm a lot! So, with a few more optimizations, here’s the weirdo_fast algorithm.
def weirdo_fast(max)
list = [ 70,836,4030,5830,7192,7912,9272,10792,17272,45356,73616, #
83312,91388,113072,243892,254012,338572,343876,388076, #
519712,539744,555616,682592,786208,1188256,1229152,1713592, #
1901728,2081824,2189024,3963968 ]
list.each do |num|
puts num if num <= max
end
if max > list[list.size-1] then weirdo_exhaustive(list[list.size-1],
max) end
end
A little faster, eh? It will start exhaustively searching if your max
is out of its range… but I warn you, my computer has been
calculating 3963970 for the last 24 hours. And, not the range up to it,
just that number. Remember, its 3^3963970 = 62_286_091_158_062_773_000
which takes a minute to calculate. Well, with my other optimizations,
its a tad faster than that… a tad.
I’m interested to see if anyone else found a much better way to test
for has_sum(num, array). It seems to me like there must be a clever
mathematical trick for getting the job done. Or perhaps the only clever
way is my “fast” algorithm. But, that’s not even that clever.
def divisors(value)
ary = []
(value/2).times do |i|
div = i + 1
ary << div if value % div == 0
end
ary
end
$bits = []
32.times do |bit|
$bits << 2 ** bit
end
def has_subset_equal_to(divs, value)
pairs = divs.zip($bits)
1.upto(2 ** divs.size - 1) do |i|
sum = 0
pairs.each{|div,b| sum+=div if (i&b)>0 }
return true if sum == value
end
false
end
def find_weird_numbers(range_min, range_max)
ary = []
range_min.upto(range_max) do |value|
divs = divisors(value)
sum = divs.inject(0){|a,b|a+b}
ary << [value, divs] if sum > value
end
res = []
ary.each do |value, divs|
if !has_subset_equal_to(divs, value)
puts “##{value} is a WEIRD NUMBER”
res << value
else
puts “##{value} is nothing”
end
end
p res
end
Here is my solution. It is pretty fast, but not quite as fast as
Rob’s. For example his takes 6.797 seconds to calculate all weird
numbers up to 50000, whereas mine takes 7.375. Our solutions are quite
similar. Some of the other solutions are REALLY SLOW though. Probably
one of the biggest optimizations was using the square root of a given
number to calculate half the divisors, then use those to get the other
half. For example if 2 is a divisor of 14, then so is 14/2 = 7. For
big numbers this can be a massive speed-up.
Ryan
class Array
def sum
inject(0) do |result, i|
result + i
end
end
end
class Integer
def weird?
# No odd numbers are weird within reasonable limits.
return false if self % 2 == 1
# A weird number is abundant but not semi-perfect.
divisors = calc_divisors
abundance = divisors.sum - 2 * self
# First make sure the number is abundant.
if abundance > 0
# Now see if the number is semi-perfect. If it is, it isn’t weird.
# First thing see if the abundance is in the divisors.
if divisors.include?(abundance)
false
else
# Now see if any combination sums of divisors yields the
abundance.
# We reject any divisors greater than the abundance and reverse
the
# result to try and get sums close to the abundance sooner.
to_search = divisors.reject{|i| i > abundance}.reverse
sum = to_search.sum
if sum == abundance
false
elsif sum < abundance
true
else
not abundance.sum_in_subset?(to_search)
end
end
else
false
end
end
def calc_divisors
res=[1]
2.upto(Math.sqrt(self).floor) do |i|
if self % i == 0
res << i
end
end
res.reverse.each do |i|
res << self / i
end
res
end
def sum_in_subset?(a)
if self < 0
false
elsif a.include?(self)
true
else
if a.length == 1
false
else
f = a.first
remaining = a[1…-1]
(self - f).sum_in_subset?(remaining) or
sum_in_subset?(remaining)
end
end
end
end
if $0 == FILE
if ARGV.length < 1
puts "Usage: #$0 "
exit(1)
end
puts “Weird numbers up to and including #{ARGV[0]}:”
70.upto(ARGV[0].to_i) do |i|
puts i if i.weird?
end
end
The Quiz was real fun and I spent quite a lot of time on it. Below
source is rather easy, but believe me I tried lots of different ways
(such as dynamic binary knapsacks etc.), most of them suck because they
need to many method calls.
The code takes about 30 seconds to find all Weird Numbers up to 10_000:
70, 836, 4030, 5830, 7192, 7912, 9272.
The code doesn’t scale rather well, the caches would need to be
optimized for that.
=end
class Integer
70, 836, 4030, 5830, 7192, 7912, 9272, 10430
def weird?
!has_semiperfect? && !weird2?
end
SEMIPERFECT = {nil => ‘shortcut’}
def weird2?(divisors=proper_divisors)
return true if divisors.any? { |x| SEMIPERFECT[x] }
if brute(self, divisors)
SEMIPERFECT[self] = true
else
false
end
end
def has_semiperfect?
SMALL_SEMIPERFECT.any? { |v| self % v == 0 }
end
def proper_divisors
d = []
sum = 0
2.upto(Math.sqrt(self)) { |i|
if self % i == 0
d << i << (self / i)
sum += i + (self / i)
end
}
return [nil] unless sum > self
d << 1
end
def brute(max, values)
values.sort!
values.delete max / 2
max = max / 2
s = values.size
(2**s).downto(0) { |n|
sum = 0
s.times { |i| sum += values[i] * n[i] }
return true if sum == max
}
false
end
end
if ARGV[0]
n = Integer(ARGV[0])
else
n = 10_000
end
Here’s my solution. It looks pretty close to others. Not too fast, but
it gets the job done. I think after I get a chance to look at other
solutions I can write a some of this better.
class Integer
def divisors
divs = []
1.upto(Math.sqrt(self).to_i) do |i|
divs += [i ,self/i].uniq if (self%i == 0)
end
divs.sort.reverse #reverse speeds things up a bit
end
def weird?
divs = self.divisors - [self]
return false if divs.sum < self
divs.each_combination do |comb|
return false if comb.sum == self
end
return true
end
end
class Array
def each_combination
(2**self.length).times do |comb|
curr = []
self.length.times do |index|
curr << self[index] if(comb[index] == 1)
end
yield curr
end
end
I’ve got an optimization I haven’t seen anyone else use yet. My
solution is roughly 20% faster than Ryan’s.
The trick is that any number of the form k*(2^m)*p where m > 1 and p
is a prime such that 2 < p < 2^(m+1) is semiperfect (according to
wikipedia). This rules out many numbers, and its cheaper than a
thorough search of subsets.
#!/usr/bin/ruby
def weird(max)
primes = sieve(max*2)
70.step(max,2){|n|
puts n if weird?(n,primes)
}
end
def weird?(n,primes)
divs = divisors(n)
abund = divs.inject(0){|a,b| a+b} - n
return false if abund <= 0
return false if spfilter(n,primes)
return false if divs.include? abund
smalldivs = divs.reverse.select{|i| i < abund}
not sum_in_subset?(smalldivs,abund)
end
def sum_in_subset?(lst,n) #p [lst,n]
return false if n < 0
return true if lst.include? n
return false if lst.size == 1
first = lst.first
rest = lst[1…-1]
sum_in_subset?(rest, n-first) or sum_in_subset?(rest,n)
end
def divisors(n)
result = []
sr = Math.sqrt(n).to_i
(2 … sr).each {|d|
if n.modulo(d) == 0
result << d
end
}
return [1] if result.empty?
hidivs = result.map {|d| n / d }.reverse
if hidivs[0] == result[-1]
[1] + result + hidivs[1…-1]
else
[1] + result + hidivs
end
end
def spfilter(n,primes)
m = 0
save_n = n
while n[0]==0
m += 1
n >>= 1
end
return false if m == 0
low = 2
high = 1 << (m+1)
primes.each {|p|
return false if p > high
if p > low
return true if n%p == 0
end
}
raise “not enough primes while checking #{save_n}”
end
Sieve of Eratosthenes
def sieve(max_prime)
candidates = Array.new(max_prime,true)
candidates[0] = candidates[1] = false
2.upto(Math.sqrt(max_prime)) {|i|
if candidates[i]
(i+i).step(max_prime,i) {|j| candidates[j] = nil}
end
}
result = []
candidates.each_with_index {|prime, i| result << i if prime }
result
end
Here is my solution (it’s optimized for statistics not for speed).
I don’t use things like someone tried all odd numbers up to 10**18
because doesn’t change the algorithm… I’ll use that in my speed
optimized version…
class Integer
$prime_factors = Hash.new # we cache prime factors…
def prime_factors position = -1
if cached = $prime_factors[self] # cached?
return cached # yes
end
if self == 1 # we have 1 we are done
return $prime_factors[self]=[] # return no factors
elsif position<0 # we havn't reached factor 5 yet
if self&1 == 0 # test factor 2
return $prime_factors[self]=[2,*(self>>1).prime_factors]
elsif self%3 == 0 # and factor 3
return $prime_factors[self]=[3,*(self/3).prime_factors]
end
end
loop do
position+=6 # increment position by 6
if position*position > self # we have a prime number return it
return $prime_factors[self]=[self]
elsif (quo,rem = divmod(position)) and rem.zero? # test 6n-1
return $prime_factors[self]=[position,*quo.prime_factors
(position-6)]
elsif (quo,rem = divmod(position+2)) and rem.zero? # and 6n+1
return $prime_factors[self]=[position+2,*quo.prime_factors
(position-6)]
end
end
end
def distinct_prime_factors # rle encode the prime factors
distinct_prime_fac = Hash.new{0} # setup the hash
prime_factors.each do |prime_factor| # fill it
distinct_prime_fac[prime_factor]+=1
end
distinct_prime_fac.to_a.sort_by{|(fac,count)|fac} # and return
it as sorted array
end
def divisors # get the divisors (not needed for divisor sum)
divs = [] # store divisors here
n = 1 # start with 1
loop do
break if n*n > self # done
if (qua,rem = divmod(n)) and rem.zero? # test for division
divs << qua # add divisors
divs << n
end
n+=1
end
divs.uniq.sort[0…-2] # we don’t want self
end
def semi_perfect? deficient=false # final test
cached_abundance = abundance
return deficient if cached_abundance < 0 # deficient return the
argument
return true if cached_abundance == 0 # perfect => semi perfect too
possible_values = {0=>true} # store all possible values in a hash
divs = self.divisors # get the divisors
div_sum_left = divs.inject(0){|a,b|a+b} # get the divisor sum
pos_2 = div_sum_left - self # this is a possibility too
divs.reverse.each do |div| # for each divisor
possible_values.keys.each do |value| # and each possible value
if value+div_sum_left < self # check wether it can reach the
number with the divisors left
possible_values.delete(value) # if not delete the number
(huge speedup)
end
new_value = value+div # we create a new possible value
including the divisor
if new_value == self or new_value == pos_2 # if it is the
number it’s semi perfect
return true
elsif new_value < self # if it’s less than the number it
could be semi perfect
possible_values[new_value]=true # add it to the possiblities
end # if it’s more than the value we can ignore it
end
div_sum_left-=div # the current divisor isn’t left anymore
end
false # we found no way to compose the number using the divisors
end
def restricted_divisor_sum # uses the formular from wikipedia
distinct_prime_factors.map do |(fac,count)|
comm_sum = 1
comm_mul = 1
count.times do
comm_sum += (comm_mul*=fac)
end
comm_sum
end.inject(1){|a,b|a*b}-self
end
def perfect? # perfect numbers have the form 2**(n-1)(2**n-1)
where n is prime (and small )
return false if self&1 == 1 # it isn’t known weather there are
odd perfect numbers… but it’s irrelevant for my algorithm
doubled = self * 2 # the perfect number is a triangular number
of the form (n(n+1))/2
doubled_root = Math.sqrt(doubled).floor # if we double it and
take the floored square root we get n
return false unless doubled == doubled_root*(doubled_root+1) #
if we don’t get n it isn’t perfect
doubled_root_string = (doubled_root+1).to_s(2) # from the first
line we know n+1 has to be of the form 2**n
return false unless doubled_root_string.count(‘1’)==1 # we check
that here
return false unless
(doubled_root_string.size-1).prime_factors.size == 1 # and n ha to be
a prime
return false unless self.abundance == 0 # if it passes all the
earlier test we check it using the abundance
true # and if it passes it’s perfect
end
def abundance
self.restricted_divisor_sum-self
end
end
require ‘benchmark’
max_num = Integer(ARGV.shift||‘1000’) rescue 1000 # read a number
from the command line
new_semi_perfects = [] # store semi perfect numbers that can’t be
constructed using other semi perfect numbers
STDOUT.sync = true
puts “Weird Numbers Up To #{max_num}:”
if test_num == next_record #stat
recorded_times << [Benchmark.times,Time.now] #stat
next_record = record_nums_left.shift #stat
end #stat
if test_num.perfect? # it's perfect
new_semi_perfects << test_num
perfect_count += 1 #stat
next
end
do_next = false
new_semi_perfects.each do |semi_per|
if test_num % semi_per == 0 # is it possible to compose the
current number using a semi-perfect number?
do_next = true # yes
composed_semi_perfect_count += 1 #stat
break
end
end
next if do_next
# no
case test_num.semi_perfect? nil # we don't care about deficient
numbers
when true # but we care about semi perfects
new_semi_perfects << test_num
new_semi_perfect_count += 1 #stat
when false # and even more about abundand non semi perfects
puts test_num
weird_count += 1 #stat
else #stat
deficient_count += 1 #stat
end
def plot(plot_data,max,step)
22.downto(0) do |k|
res = “”
res = form_float(max) if k == 22
while res.size != plot_data.size
val = plot_data[res.size]
lower_range = k*step
res << ( val >= lower_range ? (val < lower_range+step ?
‘#’ : ‘:’) : ’ ’ )
end
puts res
end
end
def powerset
for element_map in 0…(1 << self.length) do
subset = []
each_with_index do |element, index|
subset << element if element_map[index] == 1
end
yield subset
end
end
end
class Array
def sum
return self.inject { |total,x| total += x }
end
def divisors
list = []
(1…Math.sqrt(self).to_i).each do |x|
if (self / x) * x == self
list << x
list << (self / x) unless x == 1 or (self / x) == x
end
end
return list.sort
end
Well something had to generate those answers in the first place, and
for that I’d take my, Rob, or Edward’s solution any day.
I haven’t compared the Array in question with the posted sequences.
Perhaps it’s not very complete. Let me ask you this though, since
we’re talking about this: Which is easier to debug, the sequence
list or a broken calculation solution?
Well I certainly debugged mine by looking at the pre-existing list,
which was useful. But the fact that his list was missing things really
caused me pause, because if someone depended on that list to be
accurate, they would be in trouble.
Didn’t the solution also brute force answers when it left its list of
known numbers?
Yes but his brute-force algorithm is extremely slow, and his list is
wrong.
One last question: How well does your presumably fast solution do
when it goes beyond the listed sequence? Does it still find lots of
answers, quickly?
Of the three fastest solutions so far (Edward’s, Rob’s and mine), all
continue to generate answers at a similar rate, consistently. For
example, to generate up to 100000, the time was 16.687, 17.281 and
18.469 seconds (in the order listed above, i.e. mine is slowest.)
Since there are 204 weird numbers between 0 and 100000, each algorithm
generates about 11-12 per second.
I’m really not trying to be mean here and I apologize if I’m coming
off that way.
Well I’ve probably come off as mean to Hampton, and I don’t really
want to come off that way either. But it does rub me the wrong way to
have someone call their solution fast when the solution is WRONG.
I think what Hampton hit on is a simple cache optimization. It’s
fast and very effective. I don’t think we should be so quick to toss
it out as a viable approach…
His caching optimization is certainly a good idea (and fairly
obvious), though it would be better if it actually generated the cache
and reused it so that subsequent runs got faster. I may code up
something like this.
Plus he is missing a bunch of weird numbers in his list.
I haven’t compared the Array in question with the posted sequences.
Perhaps it’s not very complete. Let me ask you this though, since
we’re talking about this: Which is easier to debug, the sequence
list or a broken calculation solution?
Didn’t the solution also brute force answers when it left its list of
known numbers?
One last question: How well does your presumably fast solution do
when it goes beyond the listed sequence? Does it still find lots of
answers, quickly?
I’m really not trying to be mean here and I apologize if I’m coming
off that way.
I think what Hampton hit on is a simple cache optimization. It’s
fast and very effective. I don’t think we should be so quick to toss
it out as a viable approach…
Well something had to generate those answers in the first place, and
for that I’d take my, Rob, or Edward’s solution any day.
[snip]
His caching optimization is certainly a good idea (and fairly
obvious), though it would be better if it actually generated the cache
and reused it so that subsequent runs got faster. I may code up
something like this.
NOW you’re talking! You could just calculate them once and that’s
the best of both worlds…
It was supposed to be a funny statement. I even used a
And of course calling my “exhaustive” (hint in the name) a tad slower
is absolutely, 100% absurdist.
I used the wrong AO list as mentioned in my other response. I apologize
to all those who my not paying attention to “primitive weird numbers”
and “weird numbers” hurt in the process.
This quiz is for fun not competition.
-hampton.
PS: For further clarification, about 50% of this message is also
supposed to be somewhat amusing.