# Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 1.16 KB  |  hits: 9  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. def loop_f(n)
2.   collect_div_2 = []
3.   collect_div_3 = []
4.   collect_div_5 = []
5.   collect_div_7 = []
6.
7.   div_r = n
8.   while true
9.     div_r /= 2
10.     if div_r == 0 || div_r == 1
11.       collect_div_2 << 1
12.       break
13.     end
14.     collect_div_2 << div_r
15.   end
16.
17.   div_r = n
18.   while true
19.     div_r /= 3
20.     if div_r == 0 || div_r == 1
21.       collect_div_3 << 1
22.       break
23.     end
24.     collect_div_3 << div_r
25.   end
26.
27.   div_r = n
28.   while true
29.     div_r /= 5
30.     if div_r == 0 || div_r == 1
31.       collect_div_5 << 1
32.       break
33.     end
34.     collect_div_5 << div_r
35.   end
36.
37.   div_r = n
38.   while true
39.     div_r /= 7
40.     if div_r == 0 || div_r == 1
41.       collect_div_7 << 1
42.       break
43.     end
44.     collect_div_7 << div_r
45.   end
46.
47.   sum = 0
48.   collect_div_2.each do |div_2|
49.     collect_div_3.each do |div_3|
50.       collect_div_5.each do |div_5|
51.         collect_div_7.each do |div_7|
52.           sum += div_2 + div_3 + div_5 + div_7
53.         end
54.       end
55.     end
56.   end
57.   sum
58. end
59.
60.
61. def loop_f_t(n)
62.   t = Time.new
63.   puts loop_f(n)
64.   Time.new - t
65. end
66.
67. def gen_test_data()
68.   File.open("/tmp/recursive_to_loop_test_data.txt", "w") do |f|
69.     400000000000.times do |n|
70.       f.write(loop_f(n))
71.       f.write "\n"
72.     end
73.   end
74. end