-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojecteuler.rb
155 lines (136 loc) · 2.08 KB
/
projecteuler.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#1
=begin
total = 0
1000.times do | number |
if (number % 3 == 0) || (number % 5 == 0)
total += number
end
end
puts total
=end
=begin
#2
fibarray = [1,2]
total = 2
while fibarray[-1] + fibarray[-2] < 4000001
fibarray.push(fibarray[-1]+fibarray[-2])
if fibarray[-1] % 2 == 0
total += fibarray[-1]
end
end
puts total
=end
=begin
j = 2.0
factors = []
while j < Math.sqrt(600851475143)+1
if 600851475143 % j == 0
factors.push(j)
end
j += 1.0
end
k = 0
while k < factors.count
test = 2
while test < factors[k]
if factors[k]%test == 0
factors.delete_at(k)
k -= 1
end
test += 1
end
k+=1
end
puts factors.max
=end
=begin
def factor_search(target)
factors = []
i = 1
while i < (Math.sqrt(target)+1)
if target % i == 0
factors.push(i)
if (target/i) != i
factors.push(target/i)
end
end
i += 1
end
return factors.length
end
triangle = 1
val_to_add = 2
while factor_search(triangle) < 500
triangle += val_to_add
val_to_add += 1
if factor_search(triangle) > 100
puts "#{triangle} #{val_to_add-1} #{factor_search(triangle)}"
end
end
puts triangle
puts factor_search(triangle)
=end
=begin
#14
#longest collatz sequence
$lengths = [1]
def chainsearch(int, sequence)
if int != 1
if int.even?
next_int = int/2
else
next_int = (3*int)+1
end
sequence.push(next_int)
chainsearch(next_int, sequence)
else
$lengths.push(sequence.length)
end
end
#def sequence(int)
# if int < 1000000
# chainsearch(int, [int])
# sequence(int+1)
# else
# p $lengths
# end
#end
i = 2
while i < 1000000
chainsearch(i, [i])
i += 1
end
maximum = $lengths.max
p maximum
p $lengths.index(maximum)+1
#~25 seconds
#837799
=end
#num 16 and #num20
def digital_root(num)
next_step = digital_root_step(num)
if next_step > 9
digital_root(next_step)
else
return next_step
end
end
def digital_root_step(num)
num_arr = []
while num > 0
num_arr.push(num % 10)
num = ((num-num % 10)/10)
end
step_result = 0
num_arr.each do |i|
step_result += i
end
return step_result
end
fact = 1
i = 100
while i > 1
fact *= i
i -= 1
end
puts digital_root_step((fact))