51 lines
2.1 KiB
Text
51 lines
2.1 KiB
Text
|
|
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
|