RosettaCodeData/Task/Jacobsthal-numbers/Phix/jacobsthal-numbers.phix
2026-04-30 12:34:36 -04:00

36 lines
1.3 KiB
Text

with javascript_semantics
function jacobsthal(integer n)
return floor((power(2,n)+odd(n))/3)
end function
function jacobsthal_lucas(integer n)
return power(2,n)+power(-1,n)
end function
function jacobsthal_oblong(integer n)
return jacobsthal(n)*jacobsthal(n+1)
end function
function jba(string fmt, sequence s, integer b=5)
return {join_by(apply(true,sprintf,{{fmt},s}),1,b," ")}
end function
printf(1,"First 30 Jacobsthal numbers:\n%s\n", jba("%9d",apply(tagset(29,0),jacobsthal)))
printf(1,"First 30 Jacobsthal-Lucas numbers:\n%s\n", jba("%9d",apply(tagset(29,0),jacobsthal_lucas)))
printf(1,"First 20 Jacobsthal oblong numbers:\n%s\n",jba("%11d",apply(tagset(19,0),jacobsthal_oblong)))
--printf(1,"First 10 Jacobsthal primes:\n%s\n", jba("%d",filter(apply(tagset(31,0),jacobsthal),is_prime),1))
--hmm(""), fine, but to go further roll out gmp (and likewise should you want the three basic functions
-- to go further they'll have to look much more like the C submission above.):
include mpfr.e
mpz z = mpz_init()
integer n = 1, found = 0
printf(1,"First 20 jacobsthal primes:\n")
while found<20 do
mpz_ui_pow_ui(z,2,n)
mpz_add_ui(z,z,odd(n))
{} = mpz_fdiv_q_ui(z,z,3)
if mpz_prime(z) then
found += 1
printf(1,"%s\n",{mpz_get_str(z)})
end if
n += 1
end while