Eban numbers: Difference between revisions

Added algorithm adapted from Phix.
(Added AutoHotkey)
(Added algorithm adapted from Phix.)
Line 1,166:
 
=={{header|Nim}}==
===Exhaustive search==
{{trans|Julia}}
<lang nim>import strformat
Line 1,239 ⟶ 1,240:
Number of eban numbers up to and including 10000000: 1599
</pre>
 
===Algorithmic computation===
{{trans|Phix}}
<lang Nim>import math, strutils, strformat
 
#---------------------------------------------------------------------------------------------------
 
func ebanCount(p10: Natural): Natural =
## Return the count of eban numbers 1..10^p10.
let
n = p10 - p10 div 3
p5 = n div 2
p4 = (n + 1) div 2
result = 5^p5 * 4^p4 - 1
 
#---------------------------------------------------------------------------------------------------
 
func eban(n: Natural): bool =
## Return true if n is an eban number (only fully tested to 10e9).
if n == 0: return false
var n = n
while n != 0:
let thou = n mod 1000
if thou div 100 != 0: return false
if thou div 10 notin {0, 3, 4, 5, 6}: return false
if thou mod 10 notin {0, 2, 4, 6}: return false
n = n div 1000
result = true
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
var s: seq[Natural]
for i in 0..1000:
if eban(i): s.add(i)
echo fmt"Eban to 1000: {s.join("", "")} ({s.len} items)"
 
s.setLen(0)
for i in 1000..4000:
if eban(i): s.add(i)
echo fmt"Eban 1000..4000: {s.join("", "")} ({s.len} items)"
 
import times
let t0 = getTime()
for i in 0..21:
echo fmt"ebanCount(10^{i}): {ebanCount(i)}"
echo ""
echo fmt"Time: {getTime() - t0}"</lang>
 
{{out}}
<pre>Eban to 1000: 2, 4, 6, 30, 32, 34, 36, 40, 42, 44, 46, 50, 52, 54, 56, 60, 62, 64, 66 (19 items)
Eban 1000..4000: 2000, 2002, 2004, 2006, 2030, 2032, 2034, 2036, 2040, 2042, 2044, 2046, 2050, 2052, 2054, 2056, 2060, 2062, 2064, 2066, 4000 (21 items)
ebanCount(10^0): 0
ebanCount(10^1): 3
ebanCount(10^2): 19
ebanCount(10^3): 19
ebanCount(10^4): 79
ebanCount(10^5): 399
ebanCount(10^6): 399
ebanCount(10^7): 1599
ebanCount(10^8): 7999
ebanCount(10^9): 7999
ebanCount(10^10): 31999
ebanCount(10^11): 159999
ebanCount(10^12): 159999
ebanCount(10^13): 639999
ebanCount(10^14): 3199999
ebanCount(10^15): 3199999
ebanCount(10^16): 12799999
ebanCount(10^17): 63999999
ebanCount(10^18): 63999999
ebanCount(10^19): 255999999
ebanCount(10^20): 1279999999
ebanCount(10^21): 1279999999
 
Time: 189 microseconds and 491 nanoseconds</pre>
 
=={{header|Perl}}==
Anonymous user