RosettaCodeData/Task/Jacobsthal-numbers/Pluto/jacobsthal-numbers.pluto
2025-08-11 18:05:26 -07:00

50 lines
2.1 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

do -- find some Jacobsthal and related Numbers
local fmt = require( "fmt" ) -- RC Pluto formatting library
local int = require( "int" ) -- RC Pluto integer library - inc. some prime utilities
local maxJacobsthal <const> -- highest Jacobsthal number we will find
, maxOblong <const> -- highest Jacobsthal oblong number we will find
, maxJPrime <const> -- number of Jacobsthal primes we will find
= 29, 20, 10
local j <const> -- will hold Jacobsthal numbers
, jl <const> -- will hold Jacobsthal-Lucas numbers
, jo <const> -- will hold Jacobsthal oblong numbers
= {}, {}, {}
-- calculate the Jacobsthal Numbers and related numbers
-- Jacobsthal : J0 == 0, J1 == 1, Jn == Jn-1 + 2 × Jn-2
-- Jacobsthal-Lucas: JL0 == 2, JL1 == 1, JLn == JLn-1 + 2 × JLn-2
-- Jacobsthal oblong: JOn == Jn x Jn-1
-- Pluto arrays can be indexed from 0 but as jo is indexed from 1, we index j, jl and jo
-- from 1 to simplify printing them
j[ 1 ], j[ 2 ], jl[ 1 ], jl[ 2 ], jo[ 1 ] = 0, 1, 2, 1, 0
for n = 2, maxJacobsthal do
j[ n + 1 ] = j[ n ] + ( 2 * j[ n - 1 ] )
jl[ n + 1 ] = jl[ n ] + ( 2 * jl[ n - 1 ] )
end
for n = 1, maxOblong do
jo[ n ] = j[ n + 1 ] * j[ n ]
end
local function showNumbers( legend : string, numbers : table )
fmt.write( "First %d %s\n", # numbers, legend )
fmt.tprint( " %11d", numbers, 5, "", "" )
end
-- show the various numbers numbers
showNumbers( "Jacobsthal Numbers:", j )
showNumbers( "Jacobsthal-Lucas Numbers:", jl )
showNumbers( "Jacobsthal oblong Numbers:", jo )
-- find some prime Jacobsthal numbers
fmt.write( "First %d Jacobstal primes:\n n Jn\n", maxJPrime )
local jn1, jn2, pCount, n = j[ 2 ], j[ 1 ], 0, 2
while pCount < maxJPrime do
local jn <const> = jn1 + ( 2 * jn2 )
jn2, jn1 = jn1, jn
if int.isprime( jn ) then
++ pCount
fmt.write( "%4d: %d\n", n, jn )
end
++ n
end
end