RosettaCodeData/Task/Prime-conspiracy/Pluto/prime-conspiracy.pluto
2026-04-30 12:34:36 -04:00

38 lines
1.1 KiB
Text

local int = require "int"
local fmt = require "fmt"
local function report_transitions(trans_map, num)
local keys = trans_map:keys()
keys:sort()
fmt.print("First %s primes. Transitions prime %% 10 -> next-prime %% 10.", fmt.int(num))
for keys as key do
local count = trans_map[key]
local freq = count / num * 100
fmt.write("%d -> %d count: %9s", key // 10, key % 10, fmt.int(count))
fmt.print(" frequency: %4.2f%%", freq)
end
print()
end
-- Sieve up to the 100 millionth prime.
local sieved = int.primes(2_038_074_743, true)
local trans_map = {}
local i = 2 -- last digit of first prime (2)
local n = 2 -- index of next prime (3) in sieved
for {1e4, 1e5, 1e6, 1e7, 1e8} as num do
while n <= num do
local p = sieved[n]
-- Count transition of i -> j.
local j = p % 10
local k = i * 10 + j
local t = trans_map[k]
if !t then
trans_map[k] = 1
else
trans_map[k] = t + 1
end
i = j
n += 1
end
report_transitions(trans_map, num)
end