9 billion names of God the integer: Difference between revisions

Content added Content deleted
mNo edit summary
m (slightly faster red implementation)
Line 3,688: Line 3,688:


=={{header|Red}}==
=={{header|Red}}==
<lang Rebol>Red [Purpose: "9 billion names of god"]
<lang Rebol>Red [Purpose: "9 billion names of god with cache"]

cache: [ ;[rem start val]
name-find: function [
[1 1 1] [2 1 1] [2 2 1]
max
] [
]

reset: [cache: [[1 1 1] [2 1 1] [2 2 1]]]

name-finder: function [max /every] [
repeat row max [
repeat row max [
case [
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 [
repeat col row [
pos: reduce []
rem: start: val: 0
i: col
case [
while [i > -1] [
col = row [
append pos i
val: 1
if (not every) and (row = max) [print val + sum row-cache]
i: i - 1
]
not col = 1 [
rem: row - col
start: min rem col
val: cache-search rem start
]
true val: 1
]
]
mem: reduce [row col val]
names: 0
append/only cache mem
names: counter row col pos names
append row-cache val
prin names prin " "
if every [prin [val ""]]
]
]]]
print ""
]
]



sum-find: function [
cache-search: function [rem start] [
row
val: 0
] [
colvals: reduce []
sum: 0
repeat col row [
foreach mem cache [
if (mem/1 = rem) and (mem/2 <= start) [
pos: reduce []
append colvals mem/3
i: col
while [i > -1] [
append pos i
i: i - 1
]
]
names: 0
sum: sum + counter row col pos names
]
]
prin sum prin " "
val: sum colvals
return val
]

counter: function [
row column
pos names
] [
sofar: column
path: reduce [] ;record of what was previously added
toadd: 1
while [not toadd > (column + 1)] [
sofar: sofar + pos/:toadd
insert path pos/:toadd
case [ ;these are the conditions
sofar >= row [
if sofar = row [names: names + 1]
sofar: sofar - path/1
remove path
toadd: toadd + 1
]
(sofar < row) and (path/1 = 0) [ ;this is where toadd is subtracted from
l1i: l1pi: l1v: none ;larger than 1 indices(path, pos) and value
foreach x path [
if x > 1 [
l1i: index? find path x
l1pi: index? find pos x
l1v: x
break
]
]
either l1i = none [
toadd: toadd + 1
] [
repeat y l1i [
sofar: sofar - path/1
remove path
]
toadd: l1pi + 1
]]]]
names
]
]


print "rows:"
print "rows:"
print name-find 25
print name-finder/every 25
reduce reset
print "sum:"
print {
sum-find 23
sum:}
name-finder 23
</lang>
</lang>