9 billion names of God the integer: Difference between revisions

Replacing Red answer with Vooglaid's
mNo edit summary
(Replacing Red answer with Vooglaid's)
Line 3,688:
 
=={{header|Red}}==
<lang Rebol>
<lang Rebol>Red [Purpose: "9 billion names of god with cache"]
Red [Author: "Toomas Vooglaid"]
 
context [
cache: [ ;[rem start val]
sum-part: function [nums [block!] count [integer!]][
[1. 1. 1.] [2. 1. 1.] [2. 2. 1.]
out: 0.0
loop count [
out: out + nums/1
if empty? nums: next nums [break]
]
out
]
nums: make map! [1 [1] 2 [1 1]]
sums: make map! [1 1 2 2]
set 'names function [row /show /all][
if row < 1 [cause-error 'user 'message "Argument needs to be >= 1"]
if show [
unless nums/:row [names row]
repeat i row [either all [probe reduce [i nums/:i sums/:i]][print nums/:i]]
]
either sums/:row [sums/:row][
out: clear []
half: to integer! row / 2
if row - 1 > last: length? nums [
repeat i row - last - 1 [names last + i]
]
repeat col row - 1 [
either col = (half + 1) [
append out at nums/(row - 1) half
break
][
append out sum-part nums/(row - col) col
]
]
also sums/:row: sum nums/:row: copy out clear out
]
]
]
 
print "rows: ^/"
reset: [cache: [[1 1 1] [2 1 1] [2 2 1]]]
names/show 25
 
print "^/sums: ^/"
name-finder: function [max /every] [
probe names 23
repeat row max [
probe names 123
case [
probe names 1234
row = 1 [if every [print "1"] continue]
row = 2 [if every [prin "1 1"] continue]
every [print ""]
]
row-cache: make block! row - 1
repeat col row [
rem: start: val: 0.
case [
col = row [
val: 1.
if (not every) and (row = max) [print val + sum row-cache]
]
not col = 1 [
rem: row - col
start: min rem col
val: cache-search rem start
]
true val: 1.
]
mem: reduce [row col val]
append/only cache mem
append row-cache val
if every [prin [to integer! val ""]]
]]
reduce reset
]
 
 
cache-search: function [rem start] [
val: 0.
colvals: reduce []
foreach mem cache [
if (mem/1 = rem) and (mem/2 <= start) [
append colvals mem/3
]
if mem/1 > rem [break]
]
val: sum colvals
return val
]
 
print "rows:"
name-finder/every 25.
print {
 
sum:}
name-finder 23.
name-finder 123.
 
</lang>
Line 3,753 ⟶ 3,739:
<pre>
rows:
 
1
1 1
1.0 1 1
1.0 2.0 1 1
1.0 2.0 2.0 1 1
1.0 3.0 3.0 2.0 1 1
1.0 3.0 4.0 3.0 2.0 1 1
1.0 4.0 5.0 5.0 3.0 2.0 1 1
1.0 4.0 7.0 6.0 5.0 3.0 2.0 1 1
1.0 5.0 8.0 9.0 7.0 5.0 3.0 2.0 1 1
1.0 5.0 10.0 11.0 10.0 7.0 5.0 3.0 2.0 1 1
1.0 6.0 12.0 15.0 13.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 6.0 14.0 18.0 18.0 14.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 7.0 16.0 23.0 23.0 20.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 7.0 19.0 27.0 30.0 26.0 21.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 8.0 21.0 34.0 37.0 35.0 28.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 8.0 24.0 39.0 47.0 44.0 38.0 29.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 9.0 27.0 47.0 57.0 58.0 49.0 40.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 9.0 30.0 54.0 70.0 71.0 65.0 52.0 41.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 10.0 33.0 64.0 84.0 90.0 82.0 70.0 54.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 10.0 37.0 72.0 101.0 110.0 105.0 89.0 73.0 55.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 11.0 40.0 84.0 119.0 136.0 131.0 116.0 94.0 75.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 11.0 44.0 94.0 141.0 163.0 164.0 146.0 123.0 97.0 76.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 12.0 48.0 108.0 164.0 199.0 201.0 186.0 157.0 128.0 99.0 77.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 12.0 52.0 120.0 192.0 235.0 248.0 230.0 201.0 164.0 131.0 100.0 77.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
 
sums:
 
sum:
1255.0
2552338241.0
3230409294.0
1.5697879722373306e35
</pre>
=={{header|REXX}}==
Anonymous user