Numbers k such that the last letter of k is the same as the first letter of k+1: Difference between revisions

Content added Content deleted
m (→‎{{header|Julia}}: minor speedups)
Line 325: Line 325:
firstcache, lastcache = map(first, spelledcache), map(last, spelledcache)
firstcache, lastcache = map(first, spelledcache), map(last, spelledcache)


function firstletter(n)
""" Return first and last groupings of 3 digits, least significant first. """
n == 0 && return 'z'
function firstlastgroupings(n)
i = n % 1000
j = 0
j = 0
while n > 0
while n > 0
n, j = divrem(n, 1000)
n, j = divrem(n, 1000)
end
end
return i, j
return firstcache[j]
end
end

firstletter(n) = n == 0 ? 'z' : firstcache[firstlastgroupings(n)[end]]


function lastletter(n)
function lastletter(n)
return n % 1000 == 0 ? (n == 0 ? 'o' : n % 1_000_000 == 0 ? 'n' : 'd') :
return n % 1000 == 0 ? (n == 0 ? 'o' : n % 1_000_000 == 0 ? 'n' : 'd') :
lastcache[firstlastgroupings(n)[begin]]
lastcache[n % 1000]
end
end