Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,121 @@
-- Rosetta Code Task written in Ada
-- Task name: "Jacobsthal numbers"
-- Task URL: https://rosettacode.org/wiki/Jacobsthal_numbers
-- I increased the the number of generated Jacobsthal Numbers and
-- Jacobsthal-Lucas Numbers to 35 from the task specified 30.
-- four command line parameters are required: 35 (J_N), 35 (J_L), 20 (J_Oblong), 10 (J_Prime_Limit)
-- September 2024, R. B. E.
pragma Ada_2022;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Numerics.Big_Numbers.Big_Integers;
with Ada.Command_Line; use Ada.Command_Line;
procedure Jacobsthal_Numbers is
function Is_Prime (N : in Natural) return Boolean is
Temp : Natural := 5;
begin
if N < 2 then
return False;
end if;
if N mod 2 = 0 then
return N = 2;
end if;
if N mod 3 = 0 then
return N = 3;
end if;
while Temp * Temp <= N loop
if N mod Temp = 0 then
return False;
end if;
Temp := Temp + 2;
if N mod Temp = 0 then
return False;
end if;
Temp := Temp + 4;
end loop;
return True;
end Is_Prime;
J_N_Limit : constant Positive := Positive'Value (Argument (1)); -- should be 35 (could be more)
J_L_Limit : constant Positive := Positive'Value (Argument (2)); -- should be 35 (could be more)
J_O_Limit : constant Positive := Positive'Value (Argument (3)); -- should be 20 (could be more)
J_Prime_Limit : constant Positive := Positive'Value (Argument (4)); -- should be exactly 10
type Array_Containing_Big_Naturals is array (natural range <>) of Big_Natural;
J_N : Array_Containing_Big_Naturals (0..J_N_Limit);
J_L : Array_Containing_Big_Naturals (0..J_L_Limit);
J_Oblong : Array_Containing_Big_Naturals (0..J_O_Limit);
Big_0 : Big_Natural := To_Big_Integer (0);
Big_1 : Big_Natural := To_Big_Integer (1);
Big_2 : Big_Natural := To_Big_Integer (2);
J_Prime_Count : Natural;
begin
if Argument_Count /= 4 then
Put_Line ("Usage: ./jacobsthal_numbers 35 35 20 10");
return;
end if;
-- This section is for the Jacobsthal Numbers (generating, preserving, displaying)
J_N (0) := Big_0;
J_N (1) := Big_1;
for I in 2..J_N_Limit loop
J_N (I) := J_N (I-1) + (Big_2 * J_N (I-2));
end loop;
New_Line;
Put ("The first ");
Put (J_N_Limit, 0);
Put_Line (" Jacobsthal numbers:");
for I in 0..J_N_Limit loop
Put (To_String (J_N (I)));
end loop;
New_Line;
-- This section is for the Jacobsthal-Lucas Numbers (generating, preserving, displaying)
J_L (0) := Big_2;
J_L (1) := Big_1;
for I in 2..J_L_Limit loop
J_L (I) := J_L (I-1) + (Big_2 * J_L (I-2));
end loop;
New_Line;
Put ("The first ");
Put (J_L_Limit, 0);
Put_Line (" Jacobsthal_Lucas numbers:");
for I in 0..J_L_Limit loop
Put (To_String (J_L (I)));
end loop;
New_Line;
-- This section is for the Jacobsthal-Oblong Numbers (generating, preserving, displaying)
for I in 0..J_O_Limit loop
J_Oblong (I) := J_N (I) * J_N (I+1);
end loop;
New_Line;
Put ("The first ");
Put (J_O_Limit, 0);
Put_Line (" Jacobsthal-Oblong numbers:");
for I in 0..J_O_Limit loop
Put (To_String (J_Oblong (I)));
end loop;
New_Line;
-- This section is for the display of Jacobsthal prime numbers
New_Line;
Put ("The first ");
Put (J_Prime_Limit, 0);
Put_Line (" Jacobsthal prime numbers are:");
J_Prime_Count := 0;
-- danger here: Not all items in the J_N array can successfully be converted from Big_Integer to Integer...
for I in 3..J_N'Last loop
if Is_Prime (To_Integer ((J_N (I)))) then
J_Prime_Count := J_Prime_Count + 1;
Put (To_String (J_N (I)));
New_Line;
end if;
exit when J_Prime_Count >= J_Prime_Limit;
end loop;
New_Line;
end Jacobsthal_Numbers;

View file

@ -0,0 +1,34 @@
struct Int
def prime? # P3 Prime Generator primality test
return self | 1 == 3 if self < 5
return false if self.gcd(6) != 1
sqrt_n = Math.isqrt(self)
pc = typeof(self).new(5)
while pc <= sqrt_n
return false if self % pc == 0 || self % (pc + 2) == 0
pc += 6
end
true
end
end
def jacobsthal (n) (2_i64**n + n.bit(0))//3 end
def jacobsthal_lucas (n) 2_i64**n + (-1)**n end
def jacobsthal_oblong (n) jacobsthal(n) * jacobsthal(n+1) end
puts "First 30 Jacobsthal numbers:"
puts (0..29).map{|n| jacobsthal(n) }.join(" ")
puts "\nFirst 30 Jacobsthal-Lucas numbers: "
puts (0..29).map{|n| jacobsthal_lucas(n) }.join(" ")
puts "\nFirst 20 Jacobsthal-Oblong numbers: "
puts (0..19).map{|n| jacobsthal_oblong(n) }.join(" ")
puts "\nFirst 10 prime Jacobsthal numbers: "
res = (0..).each.compact_map do |i|
j = jacobsthal(i)
j if j.prime?
end
puts res.first(10).join(" ")

View file

@ -0,0 +1,123 @@
EXPORT Jacobsthal_numbers()
BEGIN
PRINT();
LOCAL R := "First 30 Jacobsthal numbers:";
PRINT(R);
LOCAL c, ni0, ni1, P, Q, t;
c := 0;
ni0 := 0;
ni1 := 1;
P := 1;
Q := -2;
L1 := {};
FOR J FROM 1 TO 30 DO
c := c + 1;
L1(J):=ni0;
ni0 := P*ni1 - Q*ni0;
t := ni0;
ni0 := ni1;
ni1 := t;
END;
PRINT(L1);
PRINT(" ");
LOCAL fi := 6;
LOCAL co := 5;
SALIDA(L1,R,fi,co);
LOCAL R := "First 30 Jacobsthal-Lucas numbers:";
PRINT(R);
c := 0;
ni0 := 2;
ni1 := 1;
L1 := {};
FOR J FROM 1 TO 30 DO
c := c + 1;
L1(J):=ni0;
ni0 := P*ni1 - Q*ni0;
t := ni0;
ni0 := ni1;
ni1 := t;
END;
PRINT(L1);
PRINT(" ");
fi := 6;
co := 5;
SALIDA(L1,R,fi,co);
LOCAL R := "First 20 Jacobsthal oblong numbers:";
PRINT(R);
c := 0;
ni0 := 0;
ni1 := 1;
L1 := {};
FOR J FROM 1 TO 20 DO
c := c + 1;
L1(J):=ni0*ni1;
ni0 := P*ni1 - Q*ni0;
t := ni0;
ni0 := ni1;
ni1 := t;
END;
PRINT(L1);
PRINT(" ");
fi := 5;
co := 4;
SALIDA(L1,R,fi,co);
LOCAL R := "First 10 Jacobsthal primes:";
PRINT (R);
c := 0;
ni0 := 0;
ni1 := 1;
L1 := {};
REPEAT
IF isprime(ni0) THEN
c := c + 1;
L1(c) := ni0;
END;
ni0 := P*ni1 - Q*ni0;
t := ni0;
ni0 := ni1;
ni1 := t;
UNTIL c == 10;
PRINT(L1);
LOCAL fi := 10;
LOCAL co := 1;
SALIDA(L1,R,fi,co);
END;
SALIDA(L1,R,fi,co)
BEGIN
LOCAL M := MatToList(list2mat(L1,co));
LOCAL fila := fi;
LOCAL col := co;
LOCAL x0 := 5;
LOCAL y0 := 137;
LOCAL dx;
CASE
IF co==4 THEN dx:=75 END;
IF co==5 THEN dx:=64 END;
END;
LOCAL dy := 17;
LOCAL x := {};
FOR I FROM 1 TO col DO
x(I) := x0 + (I-1)*dx;
END;
RECT_P();
TEXTOUT_P(R, x0, y0/10);
FOR I FROM 1 TO fila DO
FOR J FROM 1 TO col DO
TEXTOUT_P(
STRING(M[I,J]),
x[J],
y0 - (6-(I-1))*dy,
1
);
END;
END;
R := "ENTER";
TEXTOUT_P(R, 260, 210, 2);
WAIT(-1);
FREEZE;
END;

View file

@ -0,0 +1,74 @@
Mainwin 70 40
Print " First 30 Jacobsthal numbers:"
c = 0
ni0 = 0
ni1 = 1
P = 1
Q = -2
For j = 0 To 29
c = c + 1
Print using("#########",ni0),
If (c Mod 5) = 0 Then Print
ni0 = P*ni1 - Q*ni0
t = ni0
ni0 = ni1
ni1 = t
Next
Print
Print " First 30 Jacobsthal-Lucas numbers:"
c = 0
ni0 = 2
ni1 = 1
For j = 0 To 29
c = c + 1
Print using("#########",ni0),
If (c Mod 5) = 0 Then Print
ni0 = P*ni1 - Q*ni0
t = ni0
ni0 = ni1
ni1 = t
Next
Print
Print " First 20 Jacobsthal oblong numbers:"
c = 0
ni0 = 0
ni1 = 1
For j = 0 To 19
c = c + 1
Print using("###########",ni0*ni1),
If (c Mod 5) = 0 Then Print
ni0 = P*ni1 - Q*ni0
t = ni0
ni0 = ni1
ni1 = t
Next
Print
Print " First 10 Jacobsthal primes:"
c = 0
ni0 = 0
ni1 = 1
Do
If isPrime(ni0) Then c = c + 1: Print " "; ni0
ni0 = P*ni1 - Q*ni0
t = ni0
ni0 = ni1
ni1 = t
Loop Until c = 10
END
Function isPrime(n)
If n < 2 Then isPrime = 0: Exit Function
If n = 2 Then isPrime = 1: Exit Function
If n Mod 2 = 0 Then isPrime = 0: Exit Function
For i = 3 To Int(Sqr(n)) Step 2
If n Mod i = 0 Then
isPrime = 0
Exit Function
End If
Next i
isPrime = 1
End Function

View file

@ -1,37 +1,36 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">jacobsthal</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
with javascript_semantics
function jacobsthal(integer n)
return floor((power(2,n)+odd(n))/3)
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">jacobsthal_lucas</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function jacobsthal_lucas(integer n)
return power(2,n)+power(-1,n)
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">jacobsthal_oblong</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">jacobsthal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">jacobsthal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function jacobsthal_oblong(integer n)
return jacobsthal(n)*jacobsthal(n+1)
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">},</span><span style="color: #000000;">s</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 Jacobsthal numbers:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%9d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal</span><span style="color: #0000FF;">)))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 30 Jacobsthal-Lucas numbers:\n%s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%9d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal_lucas</span><span style="color: #0000FF;">)))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 20 Jacobsthal oblong numbers:\n%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">jba</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%11d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">jacobsthal_oblong</span><span style="color: #0000FF;">)))</span>
<span style="color: #000080;font-style:italic;">--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:</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 20 jacobsthal primes:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">found</span><span style="color: #0000FF;"><</span><span style="color: #000000;">20</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpz_ui_pow_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--
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

View file

@ -0,0 +1,85 @@
-- 25 Apr 2026
include Setting
numeric digits 250
say 'JACOBSTHAL NUMBERS'
say version
say
call Jacobsthal1 29
call Show1 'Jacobsthal',29
call Jacobsthal2 29
call Show1 'Jacobsthal-Lucas',29
call Jacobsthal3 19
call Show1 'Jacobsthal oblong',19
call Jacobsthal1 750
call Show2 'Jacobsthal',21
call Timer
exit
Jacobsthal1:
-- Get Jacobsthal numbers
procedure expose Jaco.
arg xx
a=0; b=1
Jaco.0=a; Jaco.1=b
do i=2 to xx
c=b+2*a; Jaco.i=c; a=b; b=c
end
return
Jacobsthal2:
-- Get Jacobsthal-Lucas numbers
procedure expose Jaco.
arg xx
a=2; b=1
Jaco.0=a; Jaco.1=b
do i=2 to xx
c=b+2*a; Jaco.i=c; a=b; b=c
end
return
Jacobsthal3:
-- Get Jacobsthal oblong numbers
procedure expose Jaco.
arg xx
a=0; b=1; c=3
Jaco.0=a; Jaco.1=b; Jaco.2=c
do i=3 to xx
d=3*c+6*b-8*a; Jaco.i=d; a=b; b=c; c=d
end
return
Show1:
-- Display Jacobsthal numbers
procedure expose Jaco.
parse arg header,count
say 'First' count+1 header 'numbers'
say
do i=0 to count
call CharOut ,Right(Jaco.i,12)
if i//10=4 | i//10=9 then
say
end
say
return
Show2:
-- Display Jacobsthal primes
procedure expose Jaco. Memo.
parse arg header,count
say 'First' count header 'Primes'
say
say 'No Seq Prime'
n=0
do i=0 until n=count
if Prime(Jaco.i) then do
n+=1
say Right(n,2) Right(i,3) Jaco.i '('Xpon(jaco.i)+1 'digits)'
end
end
say 'No 22 in this sequence (Seq 1709 is prime > 500 digits) could not be reached within reasonable time.'
say
return
-- Prime; Timer
include Math

View file

@ -0,0 +1,71 @@
Rebol [
title: "Rosetta code: Jacobsthal numbers"
file: %Jacobsthal_numbers.r3
url: https://rosettacode.org/wiki/Jacobsthal_numbers
note: "Based on Red language solution"
]
jacobsthal: func [
"Computes the nth Jacobsthal number via the formula"
n [number!]
][
2 ** n - (-1 ** n) / 3
]
lucas: func [
"Computes the nth Lucas number."
n [number!]
][
2 ** n + (-1 ** n)
]
oblong: func [
"Computes the product of Jacobsthal numbers for n and n+1"
n [number!]
][
multiply jacobsthal n jacobsthal n + 1
]
if unset? :prime? [
;; When native prime? function is not available...
prime?: function [
"Returns true if the input is a prime number"
n [number!] "An integer to check for primality"
][
if 2 = n [return true]
if any [n <= 1 even? n] [return false]
limit: square-root n
candidate: 3
while [candidate < limit][
if n % candidate = 0 [return false]
candidate: candidate + 2
]
true
]
]
show: function [n fn][
cols: 12
repeat i n [
prin [pad to integer! fn subtract i 1 cols]
if i % 5 = 0 [prin newline]
]
prin newline
]
print "First 30 Jacobsthal numbers:"
show 30 :jacobsthal
print "First 30 Jacobsthal-Lucas numbers:"
show 30 :lucas
print "First 20 Jacobsthal oblong numbers:"
show 20 :oblong
print "First 10 Jacobsthal primes:"
primes: n: 0
while [primes < 10][
if prime? jacob: to integer! jacobsthal n [
print jacob
primes: primes + 1
]
n: n + 1
]

View file

@ -0,0 +1,5 @@
J ← ⍥(˜⊂+⊙×₂⊸⊣₂)-2
J 30 [0 1]
J 30 [2 1]
×J 21[0 1]
▽⊸≡(=⊣⊸°/×)↘3J 33 [0 1]

View file

@ -1,59 +1,58 @@
import math.big
import math
fn jacobsthal(n u32) big.Integer {
mut t := big.one_int
t=t.lshift(n)
mut s := big.one_int
if n%2 != 0 {
s=s.neg()
}
t -= s
return t/big.integer_from_int(3)
// primality test
fn is_prime(nir i64) bool {
mut ir := i64(0)
if nir <= 1 { return false }
if nir <= 3 { return true }
if nir % 2 == 0 || nir % 3 == 0 { return false }
ir = 5
for ir * ir <= nir {
if nir % ir == 0 || nir % (ir + 2) == 0 { return false }
ir += 6
}
return true
}
fn jacobsthal_lucas(n u32) big.Integer {
mut t := big.one_int
t=t.lshift(n)
mut a := big.one_int
if n%2 != 0 {
a=a.neg()
}
return t+a
fn jacobsthal(nir i64) i64 {
return i64((math.exp2(f64(nir)) + (nir % 2))) / 3
// or return ((i64(1) << nir) + (nir % 2)) / 3 // but might get compiler notice
}
fn jacobsthal_lucas(nir i64) i64 {
mut sign := i64(1)
if nir % 2 != 0 { sign = -1 } {return i64(math.exp2(f64(nir))) + sign}
// or return (i64(1) << nir) + sign // but might get compiler notice
}
fn jacobsthal_oblong(nir i64) i64 {
return jacobsthal(nir) * jacobsthal(nir + 1)
}
fn main() {
mut jac := []big.Integer{len: 30}
println("First 30 Jacobsthal numbers:")
for i := u32(0); i < 30; i++ {
jac[i] = jacobsthal(i)
print("${jac[i]:9} ")
if (i+1)%5 == 0 {
println('')
}
}
println("\nFirst 30 Jacobsthal-Lucas numbers:")
for i := u32(0); i < 30; i++ {
print("${jacobsthal_lucas(i):9} ")
if (i+1)%5 == 0 {
println('')
}
}
println("\nFirst 20 Jacobsthal oblong numbers:")
for i := u32(0); i < 20; i++ {
print("${jac[i]*jac[i+1]:11} ")
if (i+1)%5 == 0 {
println('')
}
}
/*println("\nFirst 20 Jacobsthal primes:")
for n, count := u32(0), 0; count < 20; n++ {
j := jacobsthal(n)
if j.probably_prime(10) {
println(j)
count++
}
}*/
println("First 30 Jacobsthal numbers:")
mut jac_nums, mut jac_lucas_nums := []i64{}, []i64{}
mut jac_oblong_nums, mut prime_jac := []i64{}, []i64{}
mut ir := i64(0)
for val in i64(0) .. i64(30) {
jac_nums << jacobsthal(val)
}
println(jac_nums.str())
println("\nFirst 30 Jacobsthal-Lucas numbers:")
for val in i64(0) .. i64(30) {
jac_lucas_nums << jacobsthal_lucas(val)
}
println(jac_lucas_nums.str())
println("\nFirst 20 Jacobsthal-Oblong numbers:")
for val in i64(0) .. i64(20) {
jac_oblong_nums << jacobsthal_oblong(val)
}
println(jac_oblong_nums.str())
println("\nFirst 10 prime Jacobsthal numbers:")
for prime_jac.len < 10 {
jir := jacobsthal(ir)
if is_prime(jir) { prime_jac << jir }
ir++
}
println(prime_jac.str())
}