-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhn2.rb
More file actions
51 lines (50 loc) · 1.18 KB
/
hn2.rb
File metadata and controls
51 lines (50 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#@author Phrogz
class Integer
@@happysteps = Hash.new{ |k,v| k[v] = {} }
def happy?( base=10 )
seen = {}
num = self
until num==1 or seen[ num ]
seen[ num ] = true
num = num.to_s(base).split('').map{ |c| c.to_i(base)**2 }.inject{
|s,i| s+i }
end
num == 1
end
end
happy = Hash.new{ |h1,base|
h1[ base ] = Hash.new{ |h2, n|
if n == 1
h2[ 1 ] = true
else
h2[ n ] = :not_happy
sum_of_squares = n.to_s(base).split('').map{ |c| c.to_i(base)**2
}.inject{ |s,i| s+i }
if sum_of_squares == 1
h2[ n ] = true
else
subn = h2[ sum_of_squares ]
if subn == true
h2[ n ] = true
elsif subn == false || subn == :not_happy
h2[ n ] = h2[ sum_of_squares ] = false
end
end
end
}
}
#test
require_relative "rqc"
#RubyQuiz 93
$memx=[]
prc2 = Proc.new {
|x|
tempArr = []
until x<=0 do tempArr << (x%10)**2; x=x/10 end
sum=0
for i in 0...(tempArr.size) do sum +=tempArr[i] end
if sum==1 then $memx=[];true elsif $memx.include?(sum) then false else $memx<<sum;prc2.call(sum) end
}
RQC.qc(Fixnum)
$prm_generators[0].instance_variable_set(:@domain, 0..((2**(0.size * 8 - 2) -1)))
10000.times {p RQC.qc {|fix| p "#{fix.happy?} case:"; prc2.call(fix)==fix.happy? }}