Leonardo numbers: Difference between revisions
(→{{header|Ruby}}: used __method__ instead of explicit method name) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(116 intermediate revisions by 52 users not shown) | |||
Line 2: | Line 2: | ||
[[Category:Classic CS problems and programs]] |
[[Category:Classic CS problems and programs]] |
||
<!-- The following <math> tag doesn't render properly as it does correctly on Wikipedia. |
|||
<!-- Leonardo numbers are also known as the Leonardo series. --> |
|||
It generates some bold red error messages. |
|||
<!-- The following <math> tag doesn't render properly as it does correctly on Wikipedia:. |
|||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
||
<math> |
:<math> |
||
L(n) = |
L(n) = |
||
\begin{cases} |
\begin{cases} |
||
Line 17: | Line 17: | ||
</math> |
</math> |
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
||
--> |
|||
(end of comment) --> |
|||
The '''Leonardo numbers''' are a sequence of numbers defined by: |
|||
<big> |
|||
''Leonardo numbers'' are also known as the ''Leonardo series''. |
|||
L(0) = 1 |
|||
L(1) = 1 |
|||
L(n) = L(n-1) + L(n-2) + 1 |
|||
also |
|||
L(n) = 2 * Fib(n+1) - 1 |
|||
</big> |
|||
::: where the '''+ 1''' will herein be known as the ''add'' number. |
|||
::: where the '''FIB''' is the [[wp:Fibonacci number|Fibonacci number]]s. |
|||
The '''Leonardo numbers''' are a sequence of numbers defined by: |
|||
<big> L(0) = 1 [1<sup>st</sup> equation] </big> |
|||
<big> L(1) = 1 [2<sup>nd</sup> equation] </big> |
|||
<big> L(n) = L(n-1) + L(n-2) + 1 [3<sup>rd</sup> equation] </big> |
|||
─── also ─── |
|||
<big> L(n) = 2 * Fib(n+1) - 1 [4<sup>th</sup> equation] </big> |
|||
:::: where the '''+ 1''' will herein be known as the ''add'' number. |
|||
The task will be using the 3<sup>rd</sup> equation (above) to calculate the Leonardo numbers. |
|||
:::: where the '''FIB''' is the [[wp:Fibonacci number|Fibonacci number]]s. |
|||
This task will be using the 3<sup>rd</sup> equation (above) to calculate the Leonardo numbers. |
|||
[[wp:Edsger W. Dijkstra|Edsger W. Dijkstra]] used them as an integral part of |
|||
[[wp:Edsger W. Dijkstra|Edsger W. Dijkstra]] used Leonardo numbers as an integral part of |
|||
his [[wp:smoothsort|smoothsort]] [[wp:algorithm|algorithm]]. |
his [[wp:smoothsort|smoothsort]] [[wp:algorithm|algorithm]]. |
||
Line 62: | Line 63: | ||
Show all output here. |
Show all output here on this page. |
||
Line 75: | Line 76: | ||
* [[oeis:A001595|OEIS Leonardo numbers]] |
* [[oeis:A001595|OEIS Leonardo numbers]] |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|C++}} |
|||
<syntaxhighlight lang="11l">F leo_numbers(cnt, =l0 = 1, =l1 = 1, add = 1) |
|||
L 1..cnt |
|||
print(l0, end' ‘ ’) |
|||
(l0, l1) = (l1, l0 + l1 + add) |
|||
print() |
|||
print(‘Leonardo Numbers: ’, end' ‘’) |
|||
leo_numbers(25) |
|||
print(‘Fibonacci Numbers: ’, end' ‘’) |
|||
leo_numbers(25, 0, 1, 0)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">CARD FUNC Leonardo(BYTE n) |
|||
CARD curr,prev,tmp |
|||
IF n<=1 THEN |
|||
RETURN (1) |
|||
FI |
|||
prev=1 |
|||
curr=1 |
|||
DO |
|||
tmp=prev |
|||
prev=curr |
|||
curr==+tmp+1 |
|||
n==-1 |
|||
UNTIL n=1 |
|||
OD |
|||
RETURN (curr) |
|||
PROC Main() |
|||
BYTE n |
|||
CARD l |
|||
Put(125) ;clear screen |
|||
FOR n=0 TO 22 ;limited to 22 because of CARD limitations |
|||
DO |
|||
l=Leonardo(n) |
|||
IF n MOD 2=0 THEN |
|||
Position(2,n/2+1) |
|||
ELSE |
|||
Position(21,n/2+1) |
|||
FI |
|||
PrintF("L(%B)=%U",n,l) |
|||
OD |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Leonardo_numbers.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
L(0)=1 L(1)=1 |
|||
L(2)=3 L(3)=5 |
|||
L(4)=9 L(5)=15 |
|||
L(6)=25 L(7)=41 |
|||
L(8)=67 L(9)=109 |
|||
L(10)=177 L(11)=287 |
|||
L(12)=465 L(13)=753 |
|||
L(14)=1219 L(15)=1973 |
|||
L(16)=3193 L(17)=5167 |
|||
L(18)=8361 L(19)=13529 |
|||
L(20)=21891 L(21)=35421 |
|||
L(22)=57313 |
|||
</pre> |
|||
=={{header|Ada}}== |
|||
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
|||
procedure Leonardo is |
|||
function Leo |
|||
(N : Natural; |
|||
Step : Natural := 1; |
|||
First : Natural := 1; |
|||
Second : Natural := 1) return Natural is |
|||
L : array (0..1) of Natural := (First, Second); |
|||
begin |
|||
for i in 1 .. N loop |
|||
L := (L(1), L(0)+L(1)+Step); |
|||
end loop; |
|||
return L (0); |
|||
end Leo; |
|||
begin |
|||
Put_Line ("First 25 Leonardo numbers:"); |
|||
for I in 0 .. 24 loop |
|||
Put (Integer'Image (Leo (I))); |
|||
end loop; |
|||
New_Line; |
|||
Put_Line ("First 25 Leonardo numbers with L(0) = 0, L(1) = 1, " & |
|||
"step = 0 (fibonacci numbers):"); |
|||
for I in 0 .. 24 loop |
|||
Put (Integer'Image (Leo (I, 0, 0, 1))); |
|||
end loop; |
|||
New_Line; |
|||
end Leonardo;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Leonardo numbers with L(0) = 0, L(1) = 1, step = 0 (fibonacci numbers): |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# leonardo number parameters # |
# leonardo number parameters # |
||
MODE LEONARDO = STRUCT( INT l0, l1, add number ); |
MODE LEONARDO = STRUCT( INT l0, l1, add number ); |
||
Line 118: | Line 231: | ||
show( 25, leonardo numbers WITHLZERO 0 WITHADDNUMBER 0 ); |
show( 25, leonardo numbers WITHLZERO 0 WITHADDNUMBER 0 ); |
||
print( ( newline ) ) |
print( ( newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 124: | Line 237: | ||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
||
First 25 Leonardo numbers from 0, 1 with add number = 0 |
First 25 Leonardo numbers from 0, 1 with add number = 0 |
||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|AppleScript}}== |
|||
===Functional=== |
|||
{{Trans|Python}} (Generator version) |
|||
Drawing N items from a non-finite generator: |
|||
<syntaxhighlight lang="applescript">------------------------ GENERATOR ------------------------- |
|||
-- leo :: Int -> Int -> Int -> Generator [Int] |
|||
on leo(L0, L1, delta) |
|||
script |
|||
property x : L0 |
|||
property y : L1 |
|||
on |λ|() |
|||
set n to x |
|||
set {x, y} to {y, x + y + delta} |
|||
return n |
|||
end |λ| |
|||
end script |
|||
end leo |
|||
--------------------------- TEST --------------------------- |
|||
on run |
|||
set leonardo to leo(1, 1, 1) |
|||
set fibonacci to leo(0, 1, 0) |
|||
unlines({"First 25 Leonardo numbers:", ¬ |
|||
twoLines(take(25, leonardo)), "", ¬ |
|||
"First 25 Fibonacci numbers:", ¬ |
|||
twoLines(take(25, fibonacci))}) |
|||
end run |
|||
------------------------ FORMATTING ------------------------ |
|||
-- twoLines :: [Int] -> String |
|||
on twoLines(xs) |
|||
script row |
|||
on |λ|(ns) |
|||
tab & intercalate(", ", ns) |
|||
end |λ| |
|||
end script |
|||
return unlines(map(row, chunksOf(16, xs))) |
|||
end twoLines |
|||
------------------------- GENERIC -------------------------- |
|||
-- chunksOf :: Int -> [a] -> [[a]] |
|||
on chunksOf(n, xs) |
|||
set lng to length of xs |
|||
script go |
|||
on |λ|(a, i) |
|||
set x to (i + n) - 1 |
|||
if x ≥ lng then |
|||
a & {items i thru -1 of xs} |
|||
else |
|||
a & {items i thru x of xs} |
|||
end if |
|||
end |λ| |
|||
end script |
|||
foldl(go, {}, enumFromThenTo(1, n, lng)) |
|||
end chunksOf |
|||
-- enumFromThenTo :: Int -> Int -> Int -> [Int] |
|||
on enumFromThenTo(x1, x2, y) |
|||
set xs to {} |
|||
set d to max(1, (x2 - x1)) |
|||
repeat with i from x1 to y by d |
|||
set end of xs to i |
|||
end repeat |
|||
return xs |
|||
end enumFromThenTo |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
|||
on foldl(f, startValue, xs) |
|||
tell mReturn(f) |
|||
set v to startValue |
|||
set lng to length of xs |
|||
repeat with i from 1 to lng |
|||
set v to |λ|(v, item i of xs, i, xs) |
|||
end repeat |
|||
return v |
|||
end tell |
|||
end foldl |
|||
-- intercalate :: String -> [String] -> String |
|||
on intercalate(sep, xs) |
|||
set {dlm, my text item delimiters} to ¬ |
|||
{my text item delimiters, sep} |
|||
set s to xs as text |
|||
set my text item delimiters to dlm |
|||
return s |
|||
end intercalate |
|||
-- Lift 2nd class handler function into 1st class script wrapper |
|||
-- mReturn :: First-class m => (a -> b) -> m (a -> b) |
|||
on mReturn(f) |
|||
if class of f is script then |
|||
f |
|||
else |
|||
script |
|||
property |λ| : f |
|||
end script |
|||
end if |
|||
end mReturn |
|||
-- map :: (a -> b) -> [a] -> [b] |
|||
on map(f, xs) |
|||
tell mReturn(f) |
|||
set lng to length of xs |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to |λ|(item i of xs, i, xs) |
|||
end repeat |
|||
return lst |
|||
end tell |
|||
end map |
|||
-- max :: Ord a => a -> a -> a |
|||
on max(x, y) |
|||
if x > y then |
|||
x |
|||
else |
|||
y |
|||
end if |
|||
end max |
|||
-- take :: Int -> [a] -> [a] |
|||
-- take :: Int -> String -> String |
|||
on take(n, xs) |
|||
set c to class of xs |
|||
if list is c then |
|||
if 0 < n then |
|||
items 1 thru min(n, length of xs) of xs |
|||
else |
|||
{} |
|||
end if |
|||
else if string is c then |
|||
if 0 < n then |
|||
text 1 thru min(n, length of xs) of xs |
|||
else |
|||
"" |
|||
end if |
|||
else if script is c then |
|||
set ys to {} |
|||
repeat with i from 1 to n |
|||
set v to xs's |λ|() |
|||
if missing value is v then |
|||
return ys |
|||
else |
|||
set end of ys to v |
|||
end if |
|||
end repeat |
|||
return ys |
|||
else |
|||
missing value |
|||
end if |
|||
end take |
|||
-- unlines :: [String] -> String |
|||
on unlines(xs) |
|||
set {dlm, my text item delimiters} to ¬ |
|||
{my text item delimiters, linefeed} |
|||
set str to xs as text |
|||
set my text item delimiters to dlm |
|||
str |
|||
end unlines</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>First 25 Leonardo numbers: |
|||
1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973 |
|||
1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 |
|||
First 25 Fibonacci numbers: |
|||
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 |
|||
610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368</pre> |
|||
---- |
|||
===Idiomatic=== |
|||
Allowing optional 'L0', 'L1', and/or 'add' specs with any version of AppleScript. |
|||
<syntaxhighlight lang="applescript">-- spec: record containing none, some, or all of the 'L0', 'L1', and 'add' values. |
|||
on leonardos(spec, quantity) |
|||
-- Assign the spec values to variables, using defaults for any not given. |
|||
set {L0:a, L1:b, add:inc} to spec & {L0:1, L1:1, add:1} |
|||
-- Build the output list. |
|||
script o |
|||
property output : {a, b} |
|||
end script |
|||
repeat (quantity - 2) times |
|||
set c to a + b + inc |
|||
set end of o's output to c |
|||
set a to b |
|||
set b to c |
|||
end repeat |
|||
return o's output |
|||
end leonardos |
|||
local output, astid |
|||
set astid to AppleScript's text item delimiters |
|||
set AppleScript's text item delimiters to ", " |
|||
set output to "1st 25 Leonardos: |
|||
" & leonardos({}, 25) & " |
|||
1st 25 Fibonaccis: |
|||
" & leonardos({L0:0, L1:1, add:0}, 25) |
|||
set AppleScript's text item delimiters to astid |
|||
return output</syntaxhighlight> |
|||
{{output}} |
|||
<syntaxhighlight lang="applescript">"1st 25 Leonardos: |
|||
1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 |
|||
1st 25 Fibonaccis: |
|||
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368"</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">L: function [n l0 l1 ladd].memoize[ |
|||
(n=0)? -> l0 [ |
|||
(n=1)? -> l1 |
|||
-> (L n-1 l0 l1 ladd) + (L n-2 l0 l1 ladd) + ladd |
|||
] |
|||
] |
|||
Leonardo: function [z]-> L z 1 1 1 |
|||
print "The first 25 Leonardo numbers:" |
|||
print map 0..24 => Leonardo |
|||
print "" |
|||
print "The first 25 Leonardo numbers with L0=0, L1=1, LADD=0" |
|||
print map 0..24 'x -> L x 0 1 0</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
The first 25 Leonardo numbers with L0=0, L1=1, LADD=0 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
|||
=={{header|AutoHotkey}}== |
|||
<syntaxhighlight lang="autohotkey">Leonardo(n, L0:=1, L1:=1, step:=1){ |
|||
if n=0 |
|||
return L0 |
|||
if n=1 |
|||
return L1 |
|||
return Leonardo(n-1, L0, L1, step) + Leonardo(n-2, L0, L1, step) + step |
|||
}</syntaxhighlight> |
|||
Examples:<syntaxhighlight lang="autohotkey">output := "1st 25 Leonardo numbers, starting at L(0).`n" |
|||
loop, 25 |
|||
output .= Leonardo(A_Index-1) " " |
|||
output .= "`n`n1st 25 Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number:`n" |
|||
loop, 25 |
|||
output .= Leonardo(A_Index-1, 0, 1, 0) " " |
|||
MsgBox % output |
|||
return</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1st 25 Leonardo numbers, starting at L(0). |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
1st 25 Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
|||
=={{header|AWK}}== |
|||
<syntaxhighlight lang="awk"> |
|||
# syntax: GAWK -f LEONARDO_NUMBERS.AWK |
|||
BEGIN { |
|||
leonardo(1,1,1,"Leonardo") |
|||
leonardo(0,1,0,"Fibonacci") |
|||
exit(0) |
|||
} |
|||
function leonardo(L0,L1,step,text, i,tmp) { |
|||
printf("%s numbers (%d,%d,%d):\n",text,L0,L1,step) |
|||
for (i=1; i<=25; i++) { |
|||
if (i == 1) { |
|||
printf("%d ",L0) |
|||
} |
|||
else if (i == 2) { |
|||
printf("%d ",L1) |
|||
} |
|||
else { |
|||
printf("%d ",L0+L1+step) |
|||
tmp = L0 |
|||
L0 = L1 |
|||
L1 = tmp + L1 + step |
|||
} |
|||
} |
|||
printf("\n") |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Leonardo numbers (1,1,1): |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Fibonacci numbers (0,1,0): |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Bash}}== |
|||
<syntaxhighlight lang="bash"> |
|||
#!/bin/bash |
|||
function leonardo_number () { |
|||
L0_value=${2:-1} |
|||
L1_value=${3:-1} |
|||
Add=${4:-1} |
|||
leonardo_numbers=($L0_value $L1_value) |
|||
for (( i = 2; i < $1; ++i)) |
|||
do |
|||
leonardo_numbers+=( $((leonardo_numbers[i-1] + leonardo_numbers[i-2] + Add)) ) |
|||
done |
|||
echo "${leonardo_numbers[*]}" |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
leonardo_number 25 |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
leonardo_number 25 0 1 0 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
||
</pre> |
</pre> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="basic256"> |
|||
subroutine leonardo(L0, L1, suma, texto) |
|||
print "Numeros de " + texto + " (" + L0 + "," + L1 + "," + suma + "):" |
|||
for i = 1 to 25 |
|||
if i = 1 then |
|||
print L0 + " "; |
|||
else |
|||
if i = 2 then |
|||
print L1 + " "; |
|||
else |
|||
print L0 + L1 + suma + " "; |
|||
tmp = L0 |
|||
L0 = L1 |
|||
L1 = tmp + L1 + suma |
|||
end if |
|||
end if |
|||
next i |
|||
print chr(10) |
|||
end subroutine |
|||
#--- Programa Principal --- |
|||
call leonardo(1,1,1,"Leonardo") |
|||
call leonardo(0,1,0,"Fibonacci") |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Numeros de Leonardo (1,1,1): |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Numeros de Fibonacci (0,1,0): |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
==={{header|IS-BASIC}}=== |
|||
<syntaxhighlight lang="is-basic">100 PROGRAM "Leonardo.bas" |
|||
110 INPUT PROMPT "Enter values of L0, L1, and ADD, separated by comas: ":L0,L1,ADD |
|||
120 PRINT L0;L1; |
|||
130 FOR I=3 TO 25 |
|||
140 LET T=L1:LET L1=L1+L0+ADD:LET L0=T |
|||
160 PRINT L1; |
|||
170 NEXT |
|||
180 PRINT</syntaxhighlight> |
|||
==={{header|Sinclair ZX81 BASIC}}=== |
==={{header|Sinclair ZX81 BASIC}}=== |
||
Runs on the 1k RAM model with room to spare; hence the long(ish) variable names. The parameters are read from the keyboard. |
Runs on the 1k RAM model with room to spare; hence the long(ish) variable names. The parameters are read from the keyboard. |
||
< |
<syntaxhighlight lang="basic"> 10 INPUT L0 |
||
20 INPUT L1 |
20 INPUT L1 |
||
30 INPUT ADD |
30 INPUT ADD |
||
Line 139: | Line 627: | ||
80 LET L0=TEMP |
80 LET L0=TEMP |
||
90 PRINT " ";L1; |
90 PRINT " ";L1; |
||
100 NEXT I</ |
100 NEXT I</syntaxhighlight> |
||
{{in}} |
{{in}} |
||
<pre>1 |
<pre>1 |
||
Line 152: | Line 640: | ||
{{out}} |
{{out}} |
||
<pre> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
<pre> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
||
=={{header|BBC BASIC}}== |
|||
It's a shame when fonts don't make much of a distinction between <tt>l</tt> lower-case L and <tt>1</tt> the number One. |
|||
<syntaxhighlight lang="bbcbasic">REM >leonardo |
|||
: |
|||
PRINT "Enter values of L0, L1, and ADD, separated by commas:" |
|||
INPUT l0%, l1%, add% |
|||
PRINT l0% ' l1% |
|||
FOR i% = 3 TO 25 |
|||
temp% = l1% |
|||
l1% += l0% + add% |
|||
l0% = temp% |
|||
PRINT l1% |
|||
NEXT |
|||
PRINT |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Enter values of L0, L1, and ADD, separated by commas: |
|||
?1, 1, 1 |
|||
1 |
|||
1 |
|||
3 |
|||
5 |
|||
9 |
|||
15 |
|||
25 |
|||
41 |
|||
67 |
|||
109 |
|||
177 |
|||
287 |
|||
465 |
|||
753 |
|||
1219 |
|||
1973 |
|||
3193 |
|||
5167 |
|||
8361 |
|||
13529 |
|||
21891 |
|||
35421 |
|||
57313 |
|||
92735 |
|||
150049</pre> |
|||
<pre>Enter values of L0, L1, and ADD, separated by commas: |
|||
?0, 1, 0 |
|||
0 |
|||
1 |
|||
1 |
|||
2 |
|||
3 |
|||
5 |
|||
8 |
|||
13 |
|||
21 |
|||
34 |
|||
55 |
|||
89 |
|||
144 |
|||
233 |
|||
377 |
|||
610 |
|||
987 |
|||
1597 |
|||
2584 |
|||
4181 |
|||
6765 |
|||
10946 |
|||
17711 |
|||
28657 |
|||
46368</pre> |
|||
=={{header|Burlesque}}== |
|||
<syntaxhighlight lang="burlesque">blsq ) 1 1 1{.+\/.+}\/+]23!CCLm]wdsh |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
blsq ) 0 1 0{.+\/.+}\/+]23!CCLm]wdsh |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
This implementation fulfills the task requirements which state that the first 2 terms and the step increment should be specified. Many other implementations on this page only print out the first 25 numbers. |
This implementation fulfills the task requirements which state that the first 2 terms and the step increment should be specified. Many other implementations on this page only print out the first 25 numbers. |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
/*Abhishek Ghosh, 23rd September 2017*/ |
|||
#include<stdio.h> |
#include<stdio.h> |
||
Line 192: | Line 756: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
Output : |
||
Normal Leonardo Series : |
Normal Leonardo Series : |
||
Line 206: | Line 770: | ||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
||
</pre> |
</pre> |
||
=={{header|C sharp}}== |
|||
{{works with|C sharp|7}} |
|||
<syntaxhighlight lang="csharp">using System; |
|||
using System.Linq; |
|||
public class Program |
|||
{ |
|||
public static void Main() { |
|||
Console.WriteLine(string.Join(" ", Leonardo().Take(25))); |
|||
Console.WriteLine(string.Join(" ", Leonardo(L0: 0, L1: 1, add: 0).Take(25))); |
|||
} |
|||
public static IEnumerable<int> Leonardo(int L0 = 1, int L1 = 1, int add = 1) { |
|||
while (true) { |
|||
yield return L0; |
|||
(L0, L1) = (L1, L0 + L1 + add); |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
Line 222: | Line 812: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
||
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
||
</pre> |
|||
=={{header|Common Lisp}}== |
|||
<syntaxhighlight lang="lisp"> |
|||
;;; |
|||
;;; leo - calculates the first n number from a leo sequence. |
|||
;;; The first argument n is the number of values to return. The next three arguments a, b, add are optional. |
|||
;;; Default values provide the "original" leonardo numbers as defined in the task. |
|||
;;; a and b are the first and second element of the leonardo sequence. |
|||
;;; add is the "add number" as defined in the task definition. |
|||
;;; |
|||
(defun leo (n &optional (a 1) (b 1) (add 1)) |
|||
(labels ((iterate (n foo) |
|||
(if (zerop n) (reverse foo) |
|||
(iterate (- n 1) |
|||
(cons (+ (first foo) (second foo) add) foo))))) |
|||
(cond ((= n 1) (list a)) |
|||
(T (iterate (- n 2) (list b a)))))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
> (leo 25) |
|||
(1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) |
|||
> (leo 25 0 1 0) |
|||
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368) |
|||
</pre> |
|||
=={{header|Crystal}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="ruby">def leonardo(l_zero, l_one, add, amount) |
|||
terms = [l_zero, l_one] |
|||
while terms.size < amount |
|||
new = terms[-1] + terms[-2] |
|||
new += add |
|||
terms << new |
|||
end |
|||
terms |
|||
end |
|||
puts "First 25 Leonardo numbers: \n#{ leonardo(1,1,1,25) }" |
|||
puts "Leonardo numbers with fibonacci parameters:\n#{ leonardo(0,1,0,25) }" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
Leonardo numbers with fibonacci parameters: |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
|||
</pre> |
|||
=={{header|D}}== |
|||
{{trans|C++}} |
|||
<syntaxhighlight lang="d"> |
|||
import std.stdio; |
|||
void main() { |
|||
write("Leonardo Numbers: "); |
|||
leonardoNumbers( 25 ); |
|||
write("Fibonacci Numbers: "); |
|||
leonardoNumbers( 25, 0, 1, 0 ); |
|||
} |
|||
void leonardoNumbers(int count, int l0=1, int l1=1, int add=1) { |
|||
int t; |
|||
for (int i=0; i<count; ++i) { |
|||
write(l0, " "); |
|||
t = l0 + l1 + add; |
|||
l0 = l1; |
|||
l1 = t; |
|||
} |
|||
writeln(); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}}<pre> |
|||
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
type TIntArray = array of integer; |
|||
function GetLeonardoNumbers(Cnt,SN1,SN2,Add: integer): TIntArray; |
|||
var N: integer; |
|||
begin |
|||
SetLength(Result,Cnt); |
|||
Result[0]:=SN1; Result[1]:=SN2; |
|||
for N:=2 to Cnt-1 do |
|||
begin |
|||
Result[N]:=Result[N-1] + Result[N-2] + Add; |
|||
end; |
|||
end; |
|||
procedure TestLeonardoNumbers(Memo: TMemo); |
|||
var IA: TIntArray; |
|||
var S: string; |
|||
var I: integer; |
|||
begin |
|||
Memo.Lines.Add('Leonardo Numbers:'); |
|||
IA:=GetLeonardoNumbers(25,1,1,1); |
|||
S:=''; |
|||
for I:=0 to High(IA) do |
|||
begin |
|||
S:=S+' '+Format('%6d',[IA[I]]); |
|||
if I mod 5 = 4 then S:=S+#$0D#$0A; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
Memo.Lines.Add('Fibonacci Numbers:'); |
|||
IA:=GetLeonardoNumbers(25,0,1,0); |
|||
S:=''; |
|||
for I:=0 to High(IA) do |
|||
begin |
|||
S:=S+' '+Format('%6d',[IA[I]]); |
|||
if I mod 5 = 4 then S:=S+#$0D#$0A; |
|||
end; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Leonardo Numbers: |
|||
1 1 3 5 9 |
|||
15 25 41 67 109 |
|||
177 287 465 753 1219 |
|||
1973 3193 5167 8361 13529 |
|||
21891 35421 57313 92735 150049 |
|||
Fibonacci Numbers: |
|||
0 1 1 2 3 |
|||
5 8 13 21 34 |
|||
55 89 144 233 377 |
|||
610 987 1597 2584 4181 |
|||
6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="easylang"> |
|||
proc leonardo L0 L1 add . . |
|||
print "L0:" & L0 & " L1:" & L1 & " add:" & add |
|||
write L0 & " " |
|||
write L1 & " " |
|||
for i = 2 to 24 |
|||
tmp = L0 |
|||
L0 = L1 |
|||
L1 = tmp + L1 + add |
|||
write L1 & " " |
|||
. |
|||
print "" |
|||
. |
|||
leonardo 1 1 1 |
|||
leonardo 0 1 0 |
|||
</syntaxhighlight> |
|||
=={{header|EMal}}== |
|||
<syntaxhighlight lang="emal"> |
|||
fun leonardo = List by int n, int leo0, int leo1, int add |
|||
List leo = int[].with(n) |
|||
leo[0] = leo0 |
|||
leo[1] = leo1 |
|||
for int i = 2; i < n; ++i |
|||
leo[i] = leo[i - 1] + leo[i - 2] + add |
|||
end |
|||
return leo |
|||
end |
|||
writeLine("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:") |
|||
writeLine(leonardo(25, 1, 1, 1)) |
|||
writeLine() |
|||
writeLine("The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
|||
writeLine(leonardo(25, 0, 1, 0)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
|||
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049] |
|||
The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: |
|||
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368] |
|||
</pre> |
|||
=={{header|F#|F sharp}}== |
|||
{{trans|Haskell}} |
|||
<syntaxhighlight lang="fsharp">open System |
|||
let leo l0 l1 d = |
|||
Seq.unfold (fun (x, y) -> Some (x, (y, x + y + d))) (l0, l1) |
|||
let leonardo = leo 1 1 1 |
|||
let fibonacci = leo 0 1 0 |
|||
[<EntryPoint>] |
|||
let main _ = |
|||
let leoNums = Seq.take 25 leonardo |> Seq.chunkBySize 16 |
|||
printfn "First 25 of the (1, 1, 1) Leonardo numbers:\n%A" leoNums |
|||
Console.WriteLine() |
|||
let fibNums = Seq.take 25 fibonacci |> Seq.chunkBySize 16 |
|||
printfn "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number):\n%A" fibNums |
|||
0 // return an integer exit code</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 25 of the (1, 1, 1) Leonardo numbers: |
|||
seq |
|||
[[|1; 1; 3; 5; 9; 15; 25; 41; 67; 109; 177; 287; 465; 753; 1219; 1973|]; |
|||
[|3193; 5167; 8361; 13529; 21891; 35421; 57313; 92735; 150049|]] |
|||
First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number): |
|||
seq |
|||
[[|0; 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610|]; |
|||
[|987; 1597; 2584; 4181; 6765; 10946; 17711; 28657; 46368|]]</pre> |
|||
=={{header|Factor}}== |
|||
<syntaxhighlight lang="text">USING: fry io kernel math prettyprint sequences ; |
|||
IN: rosetta-code.leonardo-numbers |
|||
: first25-leonardo ( vector add -- seq ) |
|||
23 swap '[ dup 2 tail* sum _ + over push ] times ; |
|||
: print-leo ( seq -- ) [ pprint bl ] each nl ; |
|||
"First 25 Leonardo numbers:" print |
|||
V{ 1 1 } 1 first25-leonardo print-leo |
|||
"First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0:" print |
|||
V{ 0 1 } 0 first25-leonardo print-leo</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Fermat}}== |
|||
<syntaxhighlight lang="fermat">Func Leonardo(size, l0, l1, add) = |
|||
Array leo[1,size]; {set up as a row rather than column vector; looks nicer to print} |
|||
leo[1,1]:=l0; leo[1,2]:=l1; {fermat arrays are 1-indexed} |
|||
for i=3 to size do |
|||
leo[1,i]:=leo[1,i-2]+leo[1,i-1]+add; |
|||
od; |
|||
.; |
|||
Leonardo(25, 1, 1, 1); |
|||
[leo]; |
|||
Leonardo(25, 0, 1, 0); |
|||
[leo];</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[[ 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 ]] |
|||
[[[ 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 ]] |
|||
</pre> |
</pre> |
||
Line 232: | Line 1,082: | ||
Happily, no monster values result for the trial run, so ordinary 32-bit integers suffice. The source style uses the F90 facilities only to name the subroutine being ended (i.e. <code>END SUBROUTINE LEONARDO</code> rather than just <code>END</code>) and the I0 format code that shows an integer without a fixed space allowance, convenient in produced well-formed messages. The "$" format code signifies that the end of output from its WRITE statement should not trigger the starting of a new line for the next WRITE statement, convenient when rolling a sequence of values to a line of output one-by-one as they are concocted. Otherwise, the values would have to be accumulated in a suitable array and then written in one go. |
Happily, no monster values result for the trial run, so ordinary 32-bit integers suffice. The source style uses the F90 facilities only to name the subroutine being ended (i.e. <code>END SUBROUTINE LEONARDO</code> rather than just <code>END</code>) and the I0 format code that shows an integer without a fixed space allowance, convenient in produced well-formed messages. The "$" format code signifies that the end of output from its WRITE statement should not trigger the starting of a new line for the next WRITE statement, convenient when rolling a sequence of values to a line of output one-by-one as they are concocted. Otherwise, the values would have to be accumulated in a suitable array and then written in one go. |
||
Many versions of Fortran have enabled parameters to be optionally supplied and F90 has standardised a protocol, also introducing a declaration syntax that can specify multiple attributes in one statement which in this case would be <code>INTEGER, OPTIONAL:: AF</code> rather than two statements concerning AF. However, in a test run with <code>CALL LEONARDO(25,1,1)</code> the Compaq F90/95 compiler rejected this attempt because there was another invocation with four parameters, not three, in the same program unit. By adding the rigmarole for declaring a MODULE containing the subroutine LEONARDO, its worries |
Many versions of Fortran have enabled parameters to be optionally supplied and F90 has standardised a protocol, also introducing a declaration syntax that can specify multiple attributes in one statement which in this case would be <code>INTEGER, OPTIONAL:: AF</code> rather than two statements concerning AF. However, in a test run with <code>CALL LEONARDO(25,1,1)</code> the Compaq F90/95 compiler rejected this attempt because there was another invocation with four parameters, not three, in the same program unit. By adding the rigmarole for declaring a MODULE containing the subroutine LEONARDO, its worries would be assuaged. Many compilers (and linkers, for separately-compiled routines) would check neither the number nor the type of parameters so no such complaint would be made - but when run, the code might produce wrong results or crash. |
||
The method relies on producing a sequence of values, rather than calculating L(n) from the start each time a value from the sequence is required. < |
The method relies on producing a sequence of values, rather than calculating L(n) from the start each time a value from the sequence is required. <syntaxhighlight lang="fortran"> SUBROUTINE LEONARDO(LAST,L0,L1,AF) !Show the first LAST values of the sequence. |
||
INTEGER LAST !Limit to show. |
INTEGER LAST !Limit to show. |
||
INTEGER L0,L1 !Starting values. |
INTEGER L0,L1 !Starting values. |
||
Line 268: | Line 1,118: | ||
CALL LEONARDO(25,1,1,1) !The first 25 Leonardo numbers. |
CALL LEONARDO(25,1,1,1) !The first 25 Leonardo numbers. |
||
CALL LEONARDO(25,0,1,0) !Deviates to give the Fibonacci sequence. |
CALL LEONARDO(25,0,1,0) !Deviates to give the Fibonacci sequence. |
||
END </ |
END </syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 275: | Line 1,125: | ||
The first 25 numbers in the Leonardo sequence defined by L(0) = 0 and L(1) = 1 with L(n) = L(n - 1) + L(n - 2) + 0 |
The first 25 numbers in the Leonardo sequence defined by L(0) = 0 and L(1) = 1 with L(n) = L(n - 1) + L(n - 2) + 0 |
||
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, |
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, |
||
</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic"> |
|||
Sub leonardo(L0 As Integer, L1 As Integer, suma As Integer, texto As String) |
|||
Dim As Integer i, tmp |
|||
Print "Numeros de " &texto &" (" &L0 &"," &L1 &"," &suma &"):" |
|||
For i = 1 To 25 |
|||
If i = 1 Then |
|||
Print L0; |
|||
Elseif i = 2 Then |
|||
Print L1; |
|||
Else |
|||
Print L0 + L1 + suma; |
|||
tmp = L0 |
|||
L0 = L1 |
|||
L1 = tmp + L1 + suma |
|||
End If |
|||
Next i |
|||
Print Chr(10) |
|||
End Sub |
|||
'--- Programa Principal --- |
|||
leonardo(1,1,1,"Leonardo") |
|||
leonardo(0,1,0,"Fibonacci") |
|||
End |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Numeros de Leonardo (1,1,1): |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Numeros de Fibonacci (0,1,0): |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn LeoFiboNumbers( number as long, l0 as long, l1 as long, sum as long ) as CFArrayRef |
|||
long i, tmp |
|||
CFMutableArrayRef mutArr = fn MutableArrayWithCapacity(0) |
|||
for i = 1 to number |
|||
if i = 1 |
|||
MutableArrayAddObject( mutArr, fn StringWithFormat( @"%ld", l0 ) ) |
|||
else |
|||
if i = 2 |
|||
MutableArrayAddObject( mutArr, fn StringWithFormat( @"%ld", l1 ) ) |
|||
else |
|||
MutableArrayAddObject( mutArr, fn StringWithFormat( @"%ld", l0 + l1 + sum ) ) |
|||
tmp = L0 : l0 = l1 : l1 = tmp + l1 + sum |
|||
end if |
|||
end if |
|||
next |
|||
end fn = mutArr |
|||
void local fn CompareResults( number as long ) |
|||
CFArrayRef leonardoArr = fn LeoFiboNumbers( number, 1, 1, 1 ) |
|||
CFArrayRef fibonacciArr = fn LeoFiboNumbers( number, 0, 1, 0 ) |
|||
long i, count = fn ArrayCount( leonardoArr ) |
|||
printf @"First %ld numbers of:\n%8s%11s", number, fn StringUTF8String( @"Leonardo" ), fn StringUTF8String( @"Fibonacci" ) |
|||
for i = 0 to count - 1 |
|||
printf @"%8s%11s", fn StringUTF8String( leonardoArr[i] ), fn StringUTF8String( fibonacciArr[i] ) |
|||
next |
|||
end fn |
|||
fn CompareResults( 35 ) |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
First 35 numbers of: |
|||
Leonardo Fibonacci |
|||
1 0 |
|||
1 1 |
|||
3 1 |
|||
5 2 |
|||
9 3 |
|||
15 5 |
|||
25 8 |
|||
41 13 |
|||
67 21 |
|||
109 34 |
|||
177 55 |
|||
287 89 |
|||
465 144 |
|||
753 233 |
|||
1219 377 |
|||
1973 610 |
|||
3193 987 |
|||
5167 1597 |
|||
8361 2584 |
|||
13529 4181 |
|||
21891 6765 |
|||
35421 10946 |
|||
57313 17711 |
|||
92735 28657 |
|||
150049 46368 |
|||
242785 75025 |
|||
392835 121393 |
|||
635621 196418 |
|||
1028457 317811 |
|||
1664079 514229 |
|||
2692537 832040 |
|||
4356617 1346269 |
|||
7049155 2178309 |
|||
11405773 3524578 |
|||
18454929 5702887 |
|||
</pre> |
|||
=={{header|Go}}== |
|||
<syntaxhighlight lang="go">package main |
|||
import "fmt" |
|||
func leonardo(n, l0, l1, add int) []int { |
|||
leo := make([]int, n) |
|||
leo[0] = l0 |
|||
leo[1] = l1 |
|||
for i := 2; i < n; i++ { |
|||
leo[i] = leo[i - 1] + leo[i - 2] + add |
|||
} |
|||
return leo |
|||
} |
|||
func main() { |
|||
fmt.Println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:") |
|||
fmt.Println(leonardo(25, 1, 1, 1)) |
|||
fmt.Println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
|||
fmt.Println(leonardo(25, 0, 1, 0)) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
|||
[1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049] |
|||
The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: |
|||
[0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368] |
|||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (intercalate, unfoldr) |
||
import Data.List ( |
import Data.List.Split (chunksOf) |
||
--------------------- LEONARDO NUMBERS --------------------- |
|||
-- L0 -> L1 -> Add number -> Series (infinite) |
-- L0 -> L1 -> Add number -> Series (infinite) |
||
leo :: Integer -> Integer -> Integer -> [Integer] |
leo :: Integer -> Integer -> Integer -> [Integer] |
||
Line 292: | Line 1,284: | ||
fibonacci = leo 0 1 0 |
fibonacci = leo 0 1 0 |
||
--------------------------- TEST --------------------------- |
|||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
let twoLines = unlines . fmap (('\t' :) . show) . chunksOf 16 |
|||
(putStrLn . unlines) |
(putStrLn . unlines) |
||
[ "First 25 default (1, 1, 1) Leonardo numbers:\n" |
[ "First 25 default (1, 1, 1) Leonardo numbers:\n" |
||
, |
, f $ take 25 leonardo |
||
, "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers):\n" |
, "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers):\n" |
||
, |
, f $ take 25 fibonacci |
||
] |
] |
||
where |
|||
f = unlines . fmap (('\t' :) . intercalate ",") . chunksOf 16 . fmap show</syntaxhighlight> |
|||
{{Out}} |
{{Out}} |
||
<pre>First 25 default (1, 1, 1) Leonardo numbers: |
<pre>First 25 default (1, 1, 1) Leonardo numbers: |
||
1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973 |
|||
3193,5167,8361,13529,21891,35421,57313,92735,150049 |
|||
First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers): |
First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers): |
||
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 |
|||
987,1597,2584,4181,6765,10946,17711,28657,46368</pre> |
|||
Alternately, defining the list self-referentially instead of using unfoldr: |
|||
<syntaxhighlight lang="haskell">leo :: Integer -> Integer -> Integer -> [Integer] |
|||
leo l0 l1 d = s where |
|||
s = l0 : l1 : zipWith (\x y -> x + y + d) s (tail s)</syntaxhighlight> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang="j"> |
|||
<lang J> |
|||
leo =: (] , {.@[ + _2&{@] + {:@])^:(_2&+@{:@[) |
leo =: (] , {.@[ + _2&{@] + {:@])^:(_2&+@{:@[) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 325: | Line 1,323: | ||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
||
</pre> |
</pre> |
||
=={{header|Java}}== |
|||
{{trans|Kotlin}} |
|||
<syntaxhighlight lang="java">import java.util.Arrays; |
|||
import java.util.List; |
|||
@SuppressWarnings("SameParameterValue") |
|||
public class LeonardoNumbers { |
|||
private static List<Integer> leonardo(int n) { |
|||
return leonardo(n, 1, 1, 1); |
|||
} |
|||
private static List<Integer> leonardo(int n, int l0, int l1, int add) { |
|||
Integer[] leo = new Integer[n]; |
|||
leo[0] = l0; |
|||
leo[1] = l1; |
|||
for (int i = 2; i < n; i++) { |
|||
leo[i] = leo[i - 1] + leo[i - 2] + add; |
|||
} |
|||
return Arrays.asList(leo); |
|||
} |
|||
public static void main(String[] args) { |
|||
System.out.println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:"); |
|||
System.out.println(leonardo(25)); |
|||
System.out.println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:"); |
|||
System.out.println(leonardo(25, 0, 1, 0)); |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
|||
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]</pre> |
|||
=={{header|JavaScript}}== |
|||
===ES6=== |
|||
<syntaxhighlight lang="javascript">const leoNum = (c, l0 = 1, l1 = 1, add = 1) => |
|||
new Array(c).fill(add).reduce( |
|||
(p, c, i) => i > 1 ? ( |
|||
p.push(p[i - 1] + p[i - 2] + c) && p |
|||
) : p, [l0, l1] |
|||
); |
|||
console.log(leoNum(25)); |
|||
console.log(leoNum(25, 0, 1, 0));</syntaxhighlight> |
|||
<pre> |
|||
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]</pre> |
|||
Or, taking N terms from a non-finite Javascript generator: |
|||
{{Trans|Python}} |
|||
<syntaxhighlight lang="javascript">(() => { |
|||
'use strict'; |
|||
// leo :: Int -> Int -> Int -> Generator [Int] |
|||
function* leo(L0, L1, delta) { |
|||
let [x, y] = [L0, L1]; |
|||
while (true) { |
|||
yield x; |
|||
[x, y] = [y, delta + x + y]; |
|||
} |
|||
} |
|||
// ----------------------- TEST ------------------------ |
|||
// main :: IO () |
|||
const main = () => { |
|||
const |
|||
leonardo = leo(1, 1, 1), |
|||
fibonacci = leo(0, 1, 0); |
|||
return unlines([ |
|||
'First 25 Leonardo numbers:', |
|||
indentWrapped(take(25)(leonardo)), |
|||
'', |
|||
'First 25 Fibonacci numbers:', |
|||
indentWrapped(take(25)(fibonacci)) |
|||
]); |
|||
}; |
|||
// -------------------- FORMATTING --------------------- |
|||
// indentWrapped :: [Int] -> String |
|||
const indentWrapped = xs => |
|||
unlines( |
|||
map(x => '\t' + x.join(','))( |
|||
chunksOf(16)( |
|||
map(str)(xs) |
|||
) |
|||
) |
|||
); |
|||
// ----------------- GENERIC FUNCTIONS ----------------- |
|||
// chunksOf :: Int -> [a] -> [[a]] |
|||
const chunksOf = n => |
|||
xs => enumFromThenTo(0)(n)( |
|||
xs.length - 1 |
|||
).reduce( |
|||
(a, i) => a.concat([xs.slice(i, (n + i))]), |
|||
[] |
|||
); |
|||
// enumFromThenTo :: Int -> Int -> Int -> [Int] |
|||
const enumFromThenTo = x1 => |
|||
x2 => y => { |
|||
const d = x2 - x1; |
|||
return Array.from({ |
|||
length: Math.floor(y - x2) / d + 2 |
|||
}, (_, i) => x1 + (d * i)); |
|||
}; |
|||
// map :: (a -> b) -> [a] -> [b] |
|||
const map = f => |
|||
// The list obtained by applying f |
|||
// to each element of xs. |
|||
// (The image of xs under f). |
|||
xs => [...xs].map(f); |
|||
// str :: a -> String |
|||
const str = x => |
|||
x.toString(); |
|||
// take :: Int -> [a] -> [a] |
|||
// take :: Int -> String -> String |
|||
const take = n => |
|||
// The first n elements of a list, |
|||
// string of characters, or stream. |
|||
xs => 'GeneratorFunction' !== xs |
|||
.constructor.constructor.name ? ( |
|||
xs.slice(0, n) |
|||
) : [].concat.apply([], Array.from({ |
|||
length: n |
|||
}, () => { |
|||
const x = xs.next(); |
|||
return x.done ? [] : [x.value]; |
|||
})); |
|||
// unlines :: [String] -> String |
|||
const unlines = xs => xs.join('\n'); |
|||
// MAIN --- |
|||
return main(); |
|||
})();</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>First 25 Leonardo numbers: |
|||
1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973 |
|||
3193,5167,8361,13529,21891,35421,57313,92735,150049 |
|||
First 25 Fibonacci numbers: |
|||
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 |
|||
987,1597,2584,4181,6765,10946,17711,28657,46368</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
===Naive Implementation=== |
===Naive Implementation=== |
||
< |
<syntaxhighlight lang="jq">def Leonardo(zero; one; incr): |
||
def leo: |
def leo: |
||
if . == 0 then zero |
if . == 0 then zero |
||
Line 334: | Line 1,487: | ||
else ((.-1) |leo) + ((.-2) | leo) + incr |
else ((.-1) |leo) + ((.-2) | leo) + incr |
||
end; |
end; |
||
leo;</ |
leo;</syntaxhighlight> |
||
===Implementation with Caching=== |
===Implementation with Caching=== |
||
An array is used for caching, with `.[n]` storing the value L(n). |
An array is used for caching, with `.[n]` storing the value L(n). |
||
< |
<syntaxhighlight lang="jq">def Leonardo(zero; one; incr): |
||
def leo(n): |
def leo(n): |
||
if .[n] then . |
if .[n] then . |
||
Line 343: | Line 1,496: | ||
| .[n] = .[n-1] + .[n-2] + incr |
| .[n] = .[n-1] + .[n-2] + incr |
||
end; |
end; |
||
. as $n | [zero,one] | leo($n) | .[$n];</ |
. as $n | [zero,one] | leo($n) | .[$n];</syntaxhighlight> |
||
(To compute the sequence of Leonardo numbers L(1) ... L(n) without redundant computation, the last element of the pipeline in the last line of the function above should be dropped.) |
(To compute the sequence of Leonardo numbers L(1) ... L(n) without redundant computation, the last element of the pipeline in the last line of the function above should be dropped.) |
||
Line 349: | Line 1,502: | ||
'''Examples''' |
'''Examples''' |
||
< |
<syntaxhighlight lang="jq">[range(0;25) | Leonardo(1;1;1)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049]</pre> |
<pre>[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049]</pre> |
||
< |
<syntaxhighlight lang="jq">[range(0;25) | Leonardo(0;1;0)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]</pre> |
<pre>[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]</pre> |
||
Line 360: | Line 1,513: | ||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function L(n, add::Int=1, firsts::Vector=[1, 1]) |
||
l = max(maximum(n) .+ 1, length(firsts)) |
l = max(maximum(n) .+ 1, length(firsts)) |
||
r = Vector{Int}(l) |
r = Vector{Int}(l) |
||
Line 377: | Line 1,530: | ||
# Task 4 |
# Task 4 |
||
println("First 25 Leonardo numbers starting with [0, 1]: ", join(L(0:24, 0, [0, 1]), ", "))</ |
println("First 25 Leonardo numbers starting with [0, 1]: ", join(L(0:24, 0, [0, 1]), ", "))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 386: | Line 1,539: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun leonardo(n: Int, l0: Int = 1, l1: Int = 1, add: Int = 1): IntArray { |
fun leonardo(n: Int, l0: Int = 1, l1: Int = 1, add: Int = 1): IntArray { |
||
Line 401: | Line 1,554: | ||
println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
||
println(leonardo(25, 0, 1, 0).joinToString(" ")) |
println(leonardo(25, 0, 1, 0).joinToString(" ")) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 413: | Line 1,566: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function leoNums (n, L0, L1, add) |
||
local L0, L1, add = L0 or 1, L1 or 1, add or 1 |
local L0, L1, add = L0 or 1, L1 or 1, add or 1 |
||
local lNums, nextNum = {L0, L1} |
local lNums, nextNum = {L0, L1} |
||
Line 432: | Line 1,585: | ||
show("Leonardo numbers", leoNums(25)) |
show("Leonardo numbers", leoNums(25)) |
||
show("Fibonacci numbers", leoNums(25, 0, 1, 0))</ |
show("Fibonacci numbers", leoNums(25, 0, 1, 0))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Leonardo numbers: |
<pre>Leonardo numbers: |
||
Line 440: | Line 1,593: | ||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
||
=={{header| |
=={{header|Maple}}== |
||
<syntaxhighlight lang="maple">L := proc(n, L_0, L_1, add) |
|||
<lang perl>no warnings 'experimental::signatures'; |
|||
if n = 0 then |
|||
return L_0; |
|||
elif n = 1 then |
|||
return L_1; |
|||
else |
|||
return L(n - 1) + L(n - 2) + add; |
|||
end if; |
|||
end proc: |
|||
Leonardo := n -> (L(1, 1, 1),[seq(0..n - 1)]) |
|||
Fibonacci := n -> (L(0, 1, 0), [seq(0..n - 1)])</syntaxhighlight> |
|||
<pre>[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]</pre> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">L[0,L0_:1,___]:=L0 |
|||
L[1,L0_:1,L1_:1,___]:=L1 |
|||
L[n_,L0_:1,L1_:1,add_:1]:=L[n-1,L0,L1,add]+L[n-2,L0,L1,add]+add |
|||
L/@(Range[25]-1) |
|||
L[#,0,1,0]&/@(Range[25]-1)</syntaxhighlight> |
|||
<pre>{1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049} |
|||
{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368}</pre> |
|||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* Function that return the terms of an specified Leonardo sequence */ |
|||
leo_specified(n,L0,L1,add):=block( |
|||
if n=0 then L[n]:L0 else if n=1 then L[n]:L1 else L[n]:L[n-1]+L[n-2]+add, |
|||
L[n])$ |
|||
/* Test cases */ |
|||
/* First 25 terms of Leonardo numbers (specification (1,1,1)) */ |
|||
makelist(leo_specified(i,1,1,1),i,0,25); |
|||
/* First 25 terms of Fibonacci numbers (specification (0,1,0)) */ |
|||
makelist(leo_specified(i,0,1,0),i,0,25); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049,242785] |
|||
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025] |
|||
</pre> |
|||
=={{header|min}}== |
|||
{{works with|min|0.19.3}} |
|||
<syntaxhighlight lang="min">(over over + rolldown pop pick +) :next |
|||
(('print dip " " print! next) 25 times newline) :leo |
|||
"First 25 Leonardo numbers:" puts! |
|||
1 1 1 leo |
|||
"First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1:" puts! |
|||
0 0 1 leo</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Modula-2}}== |
|||
<syntaxhighlight lang="modula2">MODULE Leonardo; |
|||
FROM FormatString IMPORT FormatString; |
|||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar; |
|||
PROCEDURE leonardo(a,b,step,num : INTEGER); |
|||
VAR |
|||
buf : ARRAY[0..63] OF CHAR; |
|||
i,temp : INTEGER; |
|||
BEGIN |
|||
FOR i:=1 TO num DO |
|||
IF i=1 THEN |
|||
FormatString(" %i", buf, a); |
|||
WriteString(buf) |
|||
ELSIF i=2 THEN |
|||
FormatString(" %i", buf, b); |
|||
WriteString(buf) |
|||
ELSE |
|||
FormatString(" %i", buf, a+b+step); |
|||
WriteString(buf); |
|||
temp := a; |
|||
a := b; |
|||
b := temp + b + step |
|||
END |
|||
END; |
|||
WriteLn |
|||
END leonardo; |
|||
BEGIN |
|||
leonardo(1,1,1,25); |
|||
leonardo(0,1,0,25); |
|||
ReadChar |
|||
END Leonardo.</syntaxhighlight> |
|||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">import strformat |
|||
proc leonardoNumbers(count: int, L0: int = 1, |
|||
L1: int = 1, ADD: int = 1) = |
|||
var t = 0 |
|||
var (L0_loc, L1_loc) = (L0, L1) |
|||
for i in 0..<count: |
|||
write(stdout, fmt"{L0_loc:7}") |
|||
t = L0_loc + L1_loc + ADD |
|||
L0_loc = L1_loc |
|||
L1_loc = t |
|||
if i mod 5 == 4: |
|||
write(stdout, "\n") |
|||
write(stdout, "\n") |
|||
echo "Leonardo Numbers:" |
|||
leonardoNumbers(25) |
|||
echo "Fibonacci Numbers: " |
|||
leonardoNumbers(25, 0, 1, 0)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Leonardo Numbers: |
|||
1 1 3 5 9 |
|||
15 25 41 67 109 |
|||
177 287 465 753 1219 |
|||
1973 3193 5167 8361 13529 |
|||
21891 35421 57313 92735 150049 |
|||
Fibonacci Numbers: |
|||
0 1 1 2 3 |
|||
5 8 13 21 34 |
|||
55 89 144 233 377 |
|||
610 987 1597 2584 4181 |
|||
6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|OCaml}}== |
|||
<syntaxhighlight lang="ocaml">let seq_leonardo i = |
|||
let rec next b a () = Seq.Cons (a, next (a + b + i) b) in |
|||
next |
|||
let () = |
|||
let show (s, a, b, i) = |
|||
seq_leonardo i b a |> Seq.take 25 |
|||
|> Seq.fold_left (Printf.sprintf "%s %u") (Printf.sprintf "First 25 %s numbers:\n" s) |
|||
|> print_endline |
|||
in |
|||
List.iter show ["Leonardo", 1, 1, 1; "Fibonacci", 0, 1, 0]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Fibonacci numbers: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Perl}}== |
|||
<syntaxhighlight lang="perl">no warnings 'experimental::signatures'; |
|||
use feature 'signatures'; |
use feature 'signatures'; |
||
Line 453: | Line 1,766: | ||
print "Leonardo[1,1,1]: @L\n"; |
print "Leonardo[1,1,1]: @L\n"; |
||
my @F = map { leonardo($_,0,1,0) } 0..24; |
my @F = map { leonardo($_,0,1,0) } 0..24; |
||
print "Leonardo[0,1,0]: @F\n";</ |
print "Leonardo[0,1,0]: @F\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Leonardo[1,1,1]: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
<pre>Leonardo[1,1,1]: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
||
Line 459: | Line 1,772: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|Odin}}== |
||
<syntaxhighlight lang="Go"> |
|||
package main |
|||
/* imports */ |
|||
import "core:fmt" |
|||
/* main */ |
|||
main :: proc() { |
|||
fmt.println("\nThe first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:") |
|||
result := leonardo(25, 1, 1, 1) |
|||
fmt.println(result) |
|||
delete(result) |
|||
fmt.println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
|||
result = leonardo(25, 0, 1, 0) |
|||
fmt.println(result) |
|||
delete(result) |
|||
} |
|||
/* definitions */ |
|||
leonardo :: proc(n, l0, l1, add: int) -> []int { |
|||
leo := make([]int, n) |
|||
leo[0] = l0 |
|||
leo[1] = l1 |
|||
for i in 2 ..< n { |
|||
leo[i] = leo[i - 1] + leo[i - 2] + add |
|||
} |
|||
return leo |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
|||
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: |
|||
<lang perl6>sub 𝑳 ( $𝑳0 = 1, $𝑳1 = 1, $𝑳add = 1 ) { $𝑳0, $𝑳1, { $^n2 + $^n1 + $𝑳add } ... * } |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
|||
</pre> |
|||
=={{header|Phix}}== |
|||
# Part 1 |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
say "The first 25 Leonardo numbers:"; |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
put 𝑳()[^25]; |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">leonardo</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;">l1</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l2</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #000080;font-style:italic;">-- return the first n leonardo numbers, starting {l1,l2}, with step as the add number</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">l1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l2</span><span style="color: #0000FF;">}</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[$]+</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">step</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"Leonardo"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">leonardo</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)}</span> |
|||
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"Fibonacci"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">leonardo</span><span style="color: #0000FF;">(</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)}</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
|||
<pre> |
|||
{"Leonardo",{1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049}} |
|||
{"Fibonacci",{0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368}} |
|||
</pre> |
|||
=={{header|Picat}}== |
|||
<syntaxhighlight lang="picat">go => |
|||
println([leonardo(I) : I in 0..24]), |
|||
println([leonardo(0,1,0,I) : I in 0..24]). |
|||
leonardo(N) = leonardo(1,1,1,N). |
|||
table |
|||
leonardo(I1,_I2,_Add,0) = I1. |
|||
leonardo(_I1,I2,_Add,1) = I2. |
|||
leonardo(I1,I2,Add,N) = leonardo(I1,I2,Add,N-1) + leonardo(I1,I2,Add,N-2) + Add.</syntaxhighlight> |
|||
# Part 2 |
|||
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:"; |
|||
put 𝑳( 0, 1, 0 )[^25];</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049] |
|||
<pre>The first 25 Leonardo numbers: |
|||
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]</pre> |
|||
=={{header|PicoLisp}}== |
|||
<syntaxhighlight lang="picolisp">(de leo (A B C) |
|||
(default A 1 B 1 C 1) |
|||
(make |
|||
(do 25 |
|||
(inc |
|||
'B |
|||
(+ (link (swap 'A B)) C) ) ) ) ) |
|||
(println 'Leonardo (leo)) |
|||
(println 'Fibonacci (leo 0 1 0))</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Leonardo (1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) |
|||
Fibonacci (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)</pre> |
|||
=={{header|Plain English}}== |
|||
<syntaxhighlight lang="plainenglish">To run: |
|||
Start up. |
|||
Write "First 25 Leonardo numbers:" on the console. |
|||
Show 25 of the Leonardo numbers starting with 1 and 1 and using 1 for the add number. |
|||
Write "First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0:" on the console. |
|||
Show 25 of the Leonardo numbers starting with 0 and 1 and using 0 for the add number. |
|||
Wait for the escape key. |
|||
Shut down. |
|||
To show a number of the Leonardo numbers starting with a first number and a second number and using an add number for the add number: |
|||
If the number is less than 2, exit. |
|||
Privatize the number. |
|||
Privatize the first number. |
|||
Privatize the second number. |
|||
Subtract 2 from the number. |
|||
Write the first number then " " then the second number on the console without advancing. |
|||
Loop. |
|||
If a counter is past the number, write "" on the console; exit. |
|||
Swap the first number with the second number. |
|||
Put the first number plus the second number plus the add number into the second number. |
|||
Write the second number then " " on the console without advancing. |
|||
Repeat.</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
||
First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|PureBasic}}== |
|||
The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: |
|||
<syntaxhighlight lang="purebasic"> |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
|||
EnableExplicit |
|||
#N = 25 |
|||
Procedure leon_R(a.i, b.i, s.i = 1, n.i = #N) |
|||
If n>2 |
|||
Print(Space(1) + Str(a + b + s)) |
|||
ProcedureReturn leon_R(b, a + b + s, s, n-1) |
|||
EndIf |
|||
EndProcedure |
|||
If OpenConsole() |
|||
Define r$ |
|||
Print("Enter first two Leonardo numbers and increment step (separated by space) : ") |
|||
r$ = Input() |
|||
PrintN("First " + Str(#N) + " Leonardo numbers : ") |
|||
Print(StringField(r$, 1, Chr(32)) + Space(1) + |
|||
StringField(r$, 2, Chr(32))) |
|||
leon_R(Val(StringField(r$, 1, Chr(32))), |
|||
Val(StringField(r$, 2, Chr(32))), |
|||
Val(StringField(r$, 3, Chr(32)))) |
|||
r$ = Input() |
|||
EndIf |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Enter first two Leonardo numbers and increment step (separated by space) : 1 1 1 |
|||
First 25 Leonardo numbers : |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Enter first two Leonardo numbers and increment step (separated by space) : 0 1 0 |
|||
First 25 Leonardo numbers : |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Finite iteration=== |
|||
<lang python>def Leonardo(L_Zero, L_One, Add, Amount): |
|||
<syntaxhighlight lang="python">def Leonardo(L_Zero, L_One, Add, Amount): |
|||
terms = [L_Zero,L_One] |
terms = [L_Zero,L_One] |
||
while len(terms) < Amount: |
while len(terms) < Amount: |
||
Line 498: | Line 1,953: | ||
out += str(term) + " " |
out += str(term) + " " |
||
print out |
print out |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 504: | Line 1,959: | ||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
||
Leonardo numbers with fibonacci parameters: |
Leonardo numbers with fibonacci parameters: |
||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
===Non-finite generation=== |
|||
Or, for a non-finite stream of Leonardos, we can use a Python generator: |
|||
{{Works with|Python|3}} |
|||
<syntaxhighlight lang="python">'''Leonardo numbers''' |
|||
from functools import (reduce) |
|||
from itertools import (islice) |
|||
# leo :: Int -> Int -> Int -> Generator [Int] |
|||
def leo(L0, L1, delta): |
|||
'''A number series of the |
|||
Leonardo and Fibonacci pattern, |
|||
where L0 and L1 are the first two terms, |
|||
and delta = 1 for (L0, L1) == (1, 1) |
|||
yields the Leonardo series, while |
|||
delta = 0 defines the Fibonacci series.''' |
|||
(x, y) = (L0, L1) |
|||
while True: |
|||
yield x |
|||
(x, y) = (y, x + y + delta) |
|||
# main :: IO() |
|||
def main(): |
|||
'''Tests.''' |
|||
print('\n'.join([ |
|||
'First 25 Leonardo numbers:', |
|||
folded(16)(take(25)( |
|||
leo(1, 1, 1) |
|||
)), |
|||
'', |
|||
'First 25 Fibonacci numbers:', |
|||
folded(16)(take(25)( |
|||
leo(0, 1, 0) |
|||
)) |
|||
])) |
|||
# FORMATTING ---------------------------------------------- |
|||
# folded :: Int -> [a] -> String |
|||
def folded(n): |
|||
'''Long list folded to rows of n terms each.''' |
|||
return lambda xs: '[' + ('\n '.join( |
|||
str(ns)[1:-1] for ns in chunksOf(n)(xs) |
|||
) + ']') |
|||
# GENERIC ------------------------------------------------- |
|||
# chunksOf :: Int -> [a] -> [[a]] |
|||
def chunksOf(n): |
|||
'''A series of lists of length n, |
|||
subdividing the contents of xs. |
|||
Where the length of xs is not evenly divible, |
|||
the final list will be shorter than n.''' |
|||
return lambda xs: reduce( |
|||
lambda a, i: a + [xs[i:n + i]], |
|||
range(0, len(xs), n), [] |
|||
) if 0 < n else [] |
|||
# take :: Int -> [a] -> [a] |
|||
# take :: Int -> String -> String |
|||
def take(n): |
|||
'''The prefix of xs of length n, |
|||
or xs itself if n > length xs.''' |
|||
return lambda xs: ( |
|||
xs[0:n] |
|||
if isinstance(xs, list) |
|||
else list(islice(xs, n)) |
|||
) |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>First 25 Leonardo numbers: |
|||
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973 |
|||
3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
First 25 Fibonacci numbers: |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 |
|||
987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]</pre> |
|||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="quackery"> [ 1 1 1 ] is leo ( --> n n n ) |
|||
[ 0 1 0 ] is fibo ( --> n n n ) |
|||
[ 2 1 0 ] is lucaso ( --> n n n ) |
|||
[ temp put |
|||
rot times |
|||
[ tuck + |
|||
temp share + ] |
|||
temp release drop ] is nardo ( n n n n --> n ) |
|||
say "Leonardo numbers:" cr |
|||
25 times [ i^ leo nardo echo sp ] |
|||
cr cr |
|||
say "Fibonacci numbers:" cr |
|||
25 times [ i^ fibo nardo echo sp ] |
|||
cr cr |
|||
say "Lucas numbers:" cr |
|||
25 times [ i^ lucaso nardo echo sp ]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Fibonacci numbers: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
Lucas numbers: |
|||
2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349 15127 24476 39603 64079 103682 |
|||
</pre> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="rsplus"> |
|||
leonardo_numbers <- function(add = 1, l0 = 1, l1 = 1, how_many = 25) { |
|||
result <- c(l0, l1) |
|||
for (i in 3:how_many) |
|||
result <- append(result, result[[i - 1]] + result[[i - 2]] + add) |
|||
result |
|||
} |
|||
cat("First 25 Leonardo numbers\n") |
|||
cat(leonardo_numbers(), "\n") |
|||
cat("First 25 Leonardo numbers from 0, 1 with add number = 0\n") |
|||
cat(leonardo_numbers(0, 0, 1), "\n") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Leonardo numbers from 0, 1 with add number = 0 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
||
</pre> |
</pre> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (Leonardo n #:L0 (L0 1) #:L1 (L1 1) #:1+ (1+ 1)) |
(define (Leonardo n #:L0 (L0 1) #:L1 (L1 1) #:1+ (1+ 1)) |
||
(cond [(= n 0) L0] |
(cond [(= n 0) L0] |
||
Line 526: | Line 2,127: | ||
(check-equal? (Leonardo 1) 1) |
(check-equal? (Leonardo 1) 1) |
||
(check-equal? (Leonardo 2) 3) |
(check-equal? (Leonardo 2) 3) |
||
(check-equal? (Leonardo 3) 5))</ |
(check-equal? (Leonardo 3) 5))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>'(1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) |
<pre>'(1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) |
||
'(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)</pre> |
'(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<syntaxhighlight lang="raku" line>sub 𝑳 ( $𝑳0 = 1, $𝑳1 = 1, $𝑳add = 1 ) { $𝑳0, $𝑳1, { $^n2 + $^n1 + $𝑳add } ... * } |
|||
# Part 1 |
|||
say "The first 25 Leonardo numbers:"; |
|||
put 𝑳()[^25]; |
|||
# Part 2 |
|||
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:"; |
|||
put 𝑳( 0, 1, 0 )[^25];</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The first 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm computes Leonardo numbers, allowing the specification of L(0), L(1), and ADD#*/ |
||
numeric digits 500 /*just in case the user gets ka-razy. */ |
numeric digits 500 /*just in case the user gets ka-razy. */ |
||
@.=1 /*define the default for the @. array.*/ |
@.=1 /*define the default for the @. array.*/ |
||
Line 557: | Line 2,177: | ||
$=$ z /*append the just computed # to $ list.*/ |
$=$ z /*append the just computed # to $ list.*/ |
||
end /*j*/ /* [↓] elide the leading blank in $. */ |
end /*j*/ /* [↓] elide the leading blank in $. */ |
||
say strip($) /*stick a fork in it, we're all done. */</ |
say strip($) /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 577: | Line 2,197: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang="ring"> |
|||
{{incomplete|Rino| <br><br> The 2<sup>nd</sup> part of the output isn't complete. <br> The Fibonacci series computed with the 3<sup>rd</sup> equation for the Leonardo series isn't shown. <br><br>}} |
|||
# Project : Leanardo numbers |
|||
<lang ring> |
|||
# Project : Leonardo numbers |
|||
# Date : 2017/09/21 |
|||
# Author : Gal Zsolt (~ CalmoSoft ~) |
|||
# Email : <calmosoft@gmail.com> |
|||
n0 = 1 |
n0 = 1 |
||
n1 = 1 |
n1 = 1 |
||
add = 1 |
add = 1 |
||
see " |
see "First 25 Leonardo numbers:" + nl |
||
leonardo() |
|||
for i=3 to 25 |
|||
n0 = 1 |
|||
temp=n1 |
|||
n1 = 1 |
|||
n1=n0+n1+add |
|||
add = 0 |
|||
n0=temp |
|||
see "First 25 Leonardo numbers with L(0) = 0, L(1) = 1, step = 0 (fibonacci numbers):" + nl |
|||
see " "+ n1 |
|||
see "" + add + " " |
|||
next |
|||
leonardo() |
|||
</lang> |
|||
func leonardo() |
|||
see "" + n0 + " " + n1 |
|||
for i=3 to 25 |
|||
temp=n1 |
|||
n1=n0+n1+add |
|||
n0=temp |
|||
see " "+ n1 |
|||
next |
|||
see nl |
|||
</syntaxhighlight> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
||
First 25 Leonardo numbers with L(0) = 0, L(1) = 1, step = 0 (fibonacci numbers): |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 |
|||
</pre> |
</pre> |
||
=={{header|RPL}}== |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
{| class="wikitable" |
|||
! Code |
|||
! Comments |
|||
|- |
|||
| |
|||
≪ → l0 l1 add n |
|||
≪ l0 l1 2 →LIST |
|||
'''IF''' n 3 ≥ |
|||
'''THEN''' |
|||
l0 l1 2 n 1 - '''START''' |
|||
DUP ROT + add + |
|||
ROT OVER + 3 ROLLD |
|||
'''NEXT''' DROP2 |
|||
'''END''' |
|||
≫ ≫ ''''LENDO'''' STO |
|||
| |
|||
''( L(0) L(1) add n -- { L(0) .. L(n-1) } )'' |
|||
Initialize sequence |
|||
Initialise stack and loop |
|||
Calculate next L(i) |
|||
Store it into sequence list |
|||
Clean stack |
|||
|} |
|||
{{in}} |
|||
<pre> |
|||
1 1 1 25 '''LENDO''' |
|||
0 1 0 25 '''LENDO''' |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
2: { 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 } |
|||
1: { 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 } |
|||
</pre> |
|||
===One-liner=== |
|||
≪ 3 MAX → l0 l1 add n ≪ l0 l1 2 n '''START''' DUP2 + add + '''NEXT''' n 1 + →LIST ≫ ≫ ''''LEONE'''' STO |
|||
{{in}} |
|||
<pre> |
|||
1,1,1,24 '''LEONE''' |
|||
0,1,0,24 '''LEONE''' |
|||
</pre> |
|||
Same output as above. |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Enumerators are nice for this. |
Enumerators are nice for this. |
||
< |
<syntaxhighlight lang="ruby">def leonardo(l0=1, l1=1, add=1) |
||
return to_enum(__method__,l0,l1,add) unless block_given? |
return to_enum(__method__,l0,l1,add) unless block_given? |
||
loop do |
loop do |
||
Line 613: | Line 2,290: | ||
p leonardo.take(25) |
p leonardo.take(25) |
||
p leonardo(0,1,0).take(25) |
p leonardo(0,1,0).take(25) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
<pre>[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
||
</pre> |
|||
=={{header|Run BASIC}}== |
|||
<syntaxhighlight lang="runbasic">sqliteconnect #mem, ":memory:" |
|||
#mem execute("CREATE TABLE lno (name,L0,L1,ad)") |
|||
#mem execute("INSERT INTO lno VALUES('Leonardo',1,1,1),('Fibonacci',0,1,0);") |
|||
#mem execute("SELECT * FROM lno") |
|||
for j = 1 to 2 |
|||
#row = #mem #nextrow() |
|||
name$ = #row name$() |
|||
L0 = #row L0() |
|||
L1 = #row L1() |
|||
ad = #row ad() |
|||
print :print name$;" add=";ad :print" ";L0;" ";L1;" "; |
|||
for i = 3 to 25 |
|||
temp = L1 |
|||
L1 = L0 + L1 + ad |
|||
L0 = temp |
|||
print L1;" "; |
|||
next i |
|||
next j |
|||
end</syntaxhighlight> |
|||
<pre>Leonardo add=1 |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Fibonacci add=0 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust">fn leonardo(mut n0: u32, mut n1: u32, add: u32) -> impl std::iter::Iterator<Item = u32> { |
|||
std::iter::from_fn(move || { |
|||
let n = n0; |
|||
n0 = n1; |
|||
n1 += n + add; |
|||
Some(n) |
|||
}) |
|||
} |
|||
fn main() { |
|||
println!("First 25 Leonardo numbers:"); |
|||
for i in leonardo(1, 1, 1).take(25) { |
|||
print!("{} ", i); |
|||
} |
|||
println!(); |
|||
println!("First 25 Fibonacci numbers:"); |
|||
for i in leonardo(0, 1, 0).take(25) { |
|||
print!("{} ", i); |
|||
} |
|||
println!(); |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Fibonacci numbers: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def leo( n:Int, n1:Int=1, n2:Int=1, addnum:Int=1 ) : BigInt = n match { |
||
case 0 => n1 |
case 0 => n1 |
||
case 1 => n2 |
case 1 => n2 |
||
Line 633: | Line 2,367: | ||
(0 until 25) foreach { n => print( leo(n, n1=0, n2=1, addnum=0) + " " ) } |
(0 until 25) foreach { n => print( leo(n, n1=0, n2=1, addnum=0) + " " ) } |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>The first 25 Leonardo Numbers: |
<pre>The first 25 Leonardo Numbers: |
||
Line 640: | Line 2,374: | ||
The first 25 Fibonacci Numbers: |
The first 25 Fibonacci Numbers: |
||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
||
</pre> |
|||
=={{header|Seed7}}== |
|||
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
|||
const proc: leonardo (in var integer: l0, in var integer: l1, in integer: add, in integer: count) is func |
|||
local |
|||
var integer: temp is 0; |
|||
begin |
|||
for count do |
|||
write(" " <& l0); |
|||
temp := l0 + l1 + add; |
|||
l0 := l1; |
|||
l1 := temp; |
|||
end for; |
|||
writeln; |
|||
end func; |
|||
const proc: main is func |
|||
begin |
|||
write("Leonardo Numbers:"); |
|||
leonardo(1, 1, 1, 25); |
|||
write("Fibonacci Numbers:"); |
|||
leonardo(0, 1, 0, 25); |
|||
end func;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func 𝑳(n, 𝑳0 = 1, 𝑳1 = 1, 𝑳add = 1) { |
||
{ (𝑳0, 𝑳1) = (𝑳1, 𝑳0 + 𝑳1 + 𝑳add) } * n |
{ (𝑳0, 𝑳1) = (𝑳1, 𝑳0 + 𝑳1 + 𝑳add) } * n |
||
return 𝑳0 |
return 𝑳0 |
||
Line 652: | Line 2,416: | ||
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:" |
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:" |
||
say 25.of { 𝑳(_, 0, 1, 0) }</ |
say 25.of { 𝑳(_, 0, 1, 0) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 660: | Line 2,424: | ||
The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: |
The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: |
||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
||
</pre> |
|||
=={{header|Swift}}== |
|||
<syntaxhighlight lang="swift">struct Leonardo: Sequence, IteratorProtocol { |
|||
private let add : Int |
|||
private var n0: Int |
|||
private var n1: Int |
|||
init(n0: Int = 1, n1: Int = 1, add: Int = 1) { |
|||
self.n0 = n0 |
|||
self.n1 = n1 |
|||
self.add = add |
|||
} |
|||
mutating func next() -> Int? { |
|||
let n = n0 |
|||
n0 = n1 |
|||
n1 += n + add |
|||
return n |
|||
} |
|||
} |
|||
print("First 25 Leonardo numbers:") |
|||
print(Leonardo().prefix(25).map{String($0)}.joined(separator: " ")) |
|||
print("First 25 Fibonacci numbers:") |
|||
print(Leonardo(n0: 0, add: 0).prefix(25).map{String($0)}.joined(separator: " "))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 25 Leonardo numbers: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
First 25 Fibonacci numbers: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 698: | Line 2,496: | ||
Leo_Numbers = Ltemp |
Leo_Numbers = Ltemp |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>First 25 Leonardo numbers : |
<pre>First 25 Leonardo numbers : |
||
Line 706: | Line 2,504: | ||
If the first prarameter is too small : |
If the first prarameter is too small : |
||
The first parameter is too small</pre> |
The first parameter is too small</pre> |
||
=={{header|Visual Basic .NET}}== |
|||
{{trans|C#}} |
|||
<syntaxhighlight lang="vbnet">Module Module1 |
|||
Iterator Function Leonardo(Optional L0 = 1, Optional L1 = 1, Optional add = 1) As IEnumerable(Of Integer) |
|||
While True |
|||
Yield L0 |
|||
Dim t = L0 + L1 + add |
|||
L0 = L1 |
|||
L1 = t |
|||
End While |
|||
End Function |
|||
Sub Main() |
|||
Console.WriteLine(String.Join(" ", Leonardo().Take(25))) |
|||
Console.WriteLine(String.Join(" ", Leonardo(0, 1, 0).Take(25))) |
|||
End Sub |
|||
End Module</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368</pre> |
|||
=={{header|V (Vlang)}}== |
|||
{{trans|go}} |
|||
<syntaxhighlight lang="v (vlang)">fn leonardo(n int, l0 int, l1 int, add int) []int { |
|||
mut leo := []int{len: n} |
|||
leo[0] = l0 |
|||
leo[1] = l1 |
|||
for i := 2; i < n; i++ { |
|||
leo[i] = leo[i - 1] + leo[i - 2] + add |
|||
} |
|||
return leo |
|||
} |
|||
fn main() { |
|||
println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:") |
|||
println(leonardo(25, 1, 1, 1)) |
|||
println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
|||
println(leonardo(25, 0, 1, 0)) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
|||
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] |
|||
The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: |
|||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368] |
|||
</pre> |
|||
=={{header|Wren}}== |
|||
<syntaxhighlight lang="wren">var leonardo = Fn.new { |first, add, limit| |
|||
var leo = List.filled(limit, 0) |
|||
leo[0] = first[0] |
|||
leo[1] = first[1] |
|||
for (i in 2...limit) leo[i] = leo[i-1] + leo[i-2] + add |
|||
return leo |
|||
} |
|||
System.print("The first 25 Leonardo numbers with L(0) = 1, L(1) = 1 and Add = 1 are:") |
|||
for (l in leonardo.call([1, 1], 1, 25)) System.write("%(l) ") |
|||
System.print("\n\nThe first 25 Leonardo numbers with L(0) = 0, L(1) = 1 and Add = 0 are:") |
|||
for (l in leonardo.call([0, 1], 0, 25)) System.write("%(l) ") |
|||
System.print()</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 25 Leonardo numbers with L(0) = 1, L(1) = 1 and Add = 1 are: |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
The first 25 Leonardo numbers with L(0) = 0, L(1) = 1 and Add = 0 are: |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|Yabasic}}== |
|||
<syntaxhighlight lang="yabasic">limit = 25 |
|||
sub leonardo(L0, L1, suma, texto$) |
|||
local i |
|||
print "Numeros de " + texto$, " (", L0, ",", L1, ",", suma, "):" |
|||
for i = 1 to limit |
|||
if i = 1 then print L0, " "; |
|||
elsif i = 2 then print L1, " "; |
|||
else |
|||
print L0 + L1 + suma, " "; |
|||
tmp = L0 |
|||
L0 = L1 |
|||
L1 = tmp + L1 + suma |
|||
endif |
|||
next i |
|||
print chr$(10) |
|||
end sub |
|||
leonardo(1,1,1,"Leonardo") |
|||
leonardo(0,1,0,"Fibonacci") |
|||
end</syntaxhighlight> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">int N, L, L0, L1, Add; |
|||
[Text(0, "Enter L(0), L(1), Add: "); |
|||
L0:= IntIn(0); |
|||
L1:= IntIn(0); |
|||
Add:= IntIn(0); |
|||
IntOut(0, L0); ChOut(0, ^ ); |
|||
IntOut(0, L1); ChOut(0, ^ ); |
|||
for N:= 3 to 25 do |
|||
[L:= L1 + L0 + Add; |
|||
IntOut(0, L); ChOut(0, ^ ); |
|||
L0:= L1; |
|||
L1:= L; |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Enter L(0), L(1), Add: 1 1 1 |
|||
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 |
|||
Enter L(0), L(1), Add: 0 1 0 |
|||
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn leonardoNumber(n, n1=1,n2=1,addnum=1){ |
||
if(n==0) return(n1); |
if(n==0) return(n1); |
||
if(n==1) return(n2); |
if(n==1) return(n2); |
||
self.fcn(n-1,n1,n2,addnum) + self.fcn(n-2,n1,n2,addnum) + addnum |
self.fcn(n-1,n1,n2,addnum) + self.fcn(n-2,n1,n2,addnum) + addnum |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">println("The first 25 Leonardo Numbers:"); |
||
foreach n in (25){ print(leonardoNumber(n)," ") } |
foreach n in (25){ print(leonardoNumber(n)," ") } |
||
println("\n"); |
println("\n"); |
||
Line 719: | Line 2,641: | ||
println("The first 25 Fibonacci Numbers:"); |
println("The first 25 Fibonacci Numbers:"); |
||
foreach n in (25){ print(leonardoNumber(n, 0,1,0)," ") } |
foreach n in (25){ print(leonardoNumber(n, 0,1,0)," ") } |
||
println();</ |
println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 10:17, 17 December 2023
You are encouraged to solve this task according to the task description, using any language you may know.
Leonardo numbers are also known as the Leonardo series.
The Leonardo numbers are a sequence of numbers defined by:
L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 [3rd equation] ─── also ─── L(n) = 2 * Fib(n+1) - 1 [4th equation]
- where the + 1 will herein be known as the add number.
- where the FIB is the Fibonacci numbers.
This task will be using the 3rd equation (above) to calculate the Leonardo numbers.
Edsger W. Dijkstra used Leonardo numbers as an integral part of
his smoothsort algorithm.
The first few Leonardo numbers are:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 ···
- Task
-
- show the 1st 25 Leonardo numbers, starting at L(0).
- allow the first two Leonardo numbers to be specified [for L(0) and L(1)].
- allow the add number to be specified (1 is the default).
- show the 1st 25 Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number.
(The last task requirement will produce the Fibonacci numbers.)
Show all output here on this page.
- Related tasks
- See also
11l
F leo_numbers(cnt, =l0 = 1, =l1 = 1, add = 1)
L 1..cnt
print(l0, end' ‘ ’)
(l0, l1) = (l1, l0 + l1 + add)
print()
print(‘Leonardo Numbers: ’, end' ‘’)
leo_numbers(25)
print(‘Fibonacci Numbers: ’, end' ‘’)
leo_numbers(25, 0, 1, 0)
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Action!
CARD FUNC Leonardo(BYTE n)
CARD curr,prev,tmp
IF n<=1 THEN
RETURN (1)
FI
prev=1
curr=1
DO
tmp=prev
prev=curr
curr==+tmp+1
n==-1
UNTIL n=1
OD
RETURN (curr)
PROC Main()
BYTE n
CARD l
Put(125) ;clear screen
FOR n=0 TO 22 ;limited to 22 because of CARD limitations
DO
l=Leonardo(n)
IF n MOD 2=0 THEN
Position(2,n/2+1)
ELSE
Position(21,n/2+1)
FI
PrintF("L(%B)=%U",n,l)
OD
RETURN
- Output:
Screenshot from Atari 8-bit computer
L(0)=1 L(1)=1 L(2)=3 L(3)=5 L(4)=9 L(5)=15 L(6)=25 L(7)=41 L(8)=67 L(9)=109 L(10)=177 L(11)=287 L(12)=465 L(13)=753 L(14)=1219 L(15)=1973 L(16)=3193 L(17)=5167 L(18)=8361 L(19)=13529 L(20)=21891 L(21)=35421 L(22)=57313
Ada
with Ada.Text_IO; use Ada.Text_IO;
procedure Leonardo is
function Leo
(N : Natural;
Step : Natural := 1;
First : Natural := 1;
Second : Natural := 1) return Natural is
L : array (0..1) of Natural := (First, Second);
begin
for i in 1 .. N loop
L := (L(1), L(0)+L(1)+Step);
end loop;
return L (0);
end Leo;
begin
Put_Line ("First 25 Leonardo numbers:");
for I in 0 .. 24 loop
Put (Integer'Image (Leo (I)));
end loop;
New_Line;
Put_Line ("First 25 Leonardo numbers with L(0) = 0, L(1) = 1, " &
"step = 0 (fibonacci numbers):");
for I in 0 .. 24 loop
Put (Integer'Image (Leo (I, 0, 0, 1)));
end loop;
New_Line;
end Leonardo;
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers with L(0) = 0, L(1) = 1, step = 0 (fibonacci numbers): 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
ALGOL 68
BEGIN
# leonardo number parameters #
MODE LEONARDO = STRUCT( INT l0, l1, add number );
# default leonardo number parameters #
LEONARDO leonardo numbers = LEONARDO( 1, 1, 1 );
# operators to allow us to specify non-default parameters #
PRIO WITHLZERO = 9, WITHLONE = 9, WITHADDNUMBER = 9;
OP WITHLZERO = ( LEONARDO parameters, INT l0 )LEONARDO:
LEONARDO( l0, l1 OF parameters, add number OF parameters );
OP WITHLONE = ( LEONARDO parameters, INT l1 )LEONARDO:
LEONARDO( l0 OF parameters, l1, add number OF parameters );
OP WITHADDNUMBER = ( LEONARDO parameters, INT add number )LEONARDO:
LEONARDO( l0 OF parameters, l1 OF parameters, add number );
# show the first n Leonardo numbers with the specified parameters #
PROC show = ( INT n, LEONARDO parameters )VOID:
IF n > 0 THEN
INT l0 = l0 OF parameters;
INT l1 = l1 OF parameters;
INT add number = add number OF parameters;
print( ( whole( l0, 0 ), " " ) );
IF n > 1 THEN
print( ( whole( l1, 0 ), " " ) );
INT lp := l0;
INT ln := l1;
FROM 2 TO n - 1 DO
INT next = ln + lp + add number;
lp := ln;
ln := next;
print( ( whole( ln, 0 ), " " ) )
OD
FI
FI # show # ;
# first series #
print( ( "First 25 Leonardo numbers", newline ) );
show( 25, leonardo numbers );
print( ( newline ) );
# second series #
print( ( "First 25 Leonardo numbers from 0, 1 with add number = 0", newline ) );
show( 25, leonardo numbers WITHLZERO 0 WITHADDNUMBER 0 );
print( ( newline ) )
END
- Output:
First 25 Leonardo numbers 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers from 0, 1 with add number = 0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
AppleScript
Functional
(Generator version)
Drawing N items from a non-finite generator:
------------------------ GENERATOR -------------------------
-- leo :: Int -> Int -> Int -> Generator [Int]
on leo(L0, L1, delta)
script
property x : L0
property y : L1
on |λ|()
set n to x
set {x, y} to {y, x + y + delta}
return n
end |λ|
end script
end leo
--------------------------- TEST ---------------------------
on run
set leonardo to leo(1, 1, 1)
set fibonacci to leo(0, 1, 0)
unlines({"First 25 Leonardo numbers:", ¬
twoLines(take(25, leonardo)), "", ¬
"First 25 Fibonacci numbers:", ¬
twoLines(take(25, fibonacci))})
end run
------------------------ FORMATTING ------------------------
-- twoLines :: [Int] -> String
on twoLines(xs)
script row
on |λ|(ns)
tab & intercalate(", ", ns)
end |λ|
end script
return unlines(map(row, chunksOf(16, xs)))
end twoLines
------------------------- GENERIC --------------------------
-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(n, xs)
set lng to length of xs
script go
on |λ|(a, i)
set x to (i + n) - 1
if x ≥ lng then
a & {items i thru -1 of xs}
else
a & {items i thru x of xs}
end if
end |λ|
end script
foldl(go, {}, enumFromThenTo(1, n, lng))
end chunksOf
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
on enumFromThenTo(x1, x2, y)
set xs to {}
set d to max(1, (x2 - x1))
repeat with i from x1 to y by d
set end of xs to i
end repeat
return xs
end enumFromThenTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- intercalate :: String -> [String] -> String
on intercalate(sep, xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, sep}
set s to xs as text
set my text item delimiters to dlm
return s
end intercalate
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
set c to class of xs
if list is c then
if 0 < n then
items 1 thru min(n, length of xs) of xs
else
{}
end if
else if string is c then
if 0 < n then
text 1 thru min(n, length of xs) of xs
else
""
end if
else if script is c then
set ys to {}
repeat with i from 1 to n
set v to xs's |λ|()
if missing value is v then
return ys
else
set end of ys to v
end if
end repeat
return ys
else
missing value
end if
end take
-- unlines :: [String] -> String
on unlines(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set str to xs as text
set my text item delimiters to dlm
str
end unlines
- Output:
First 25 Leonardo numbers: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 First 25 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368
Idiomatic
Allowing optional 'L0', 'L1', and/or 'add' specs with any version of AppleScript.
-- spec: record containing none, some, or all of the 'L0', 'L1', and 'add' values.
on leonardos(spec, quantity)
-- Assign the spec values to variables, using defaults for any not given.
set {L0:a, L1:b, add:inc} to spec & {L0:1, L1:1, add:1}
-- Build the output list.
script o
property output : {a, b}
end script
repeat (quantity - 2) times
set c to a + b + inc
set end of o's output to c
set a to b
set b to c
end repeat
return o's output
end leonardos
local output, astid
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set output to "1st 25 Leonardos:
" & leonardos({}, 25) & "
1st 25 Fibonaccis:
" & leonardos({L0:0, L1:1, add:0}, 25)
set AppleScript's text item delimiters to astid
return output
- Output:
"1st 25 Leonardos:
1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049
1st 25 Fibonaccis:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368"
Arturo
L: function [n l0 l1 ladd].memoize[
(n=0)? -> l0 [
(n=1)? -> l1
-> (L n-1 l0 l1 ladd) + (L n-2 l0 l1 ladd) + ladd
]
]
Leonardo: function [z]-> L z 1 1 1
print "The first 25 Leonardo numbers:"
print map 0..24 => Leonardo
print ""
print "The first 25 Leonardo numbers with L0=0, L1=1, LADD=0"
print map 0..24 'x -> L x 0 1 0
- Output:
The first 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Leonardo numbers with L0=0, L1=1, LADD=0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
AutoHotkey
Leonardo(n, L0:=1, L1:=1, step:=1){
if n=0
return L0
if n=1
return L1
return Leonardo(n-1, L0, L1, step) + Leonardo(n-2, L0, L1, step) + step
}
Examples:
output := "1st 25 Leonardo numbers, starting at L(0).`n"
loop, 25
output .= Leonardo(A_Index-1) " "
output .= "`n`n1st 25 Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number:`n"
loop, 25
output .= Leonardo(A_Index-1, 0, 1, 0) " "
MsgBox % output
return
- Output:
1st 25 Leonardo numbers, starting at L(0). 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 1st 25 Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
AWK
# syntax: GAWK -f LEONARDO_NUMBERS.AWK
BEGIN {
leonardo(1,1,1,"Leonardo")
leonardo(0,1,0,"Fibonacci")
exit(0)
}
function leonardo(L0,L1,step,text, i,tmp) {
printf("%s numbers (%d,%d,%d):\n",text,L0,L1,step)
for (i=1; i<=25; i++) {
if (i == 1) {
printf("%d ",L0)
}
else if (i == 2) {
printf("%d ",L1)
}
else {
printf("%d ",L0+L1+step)
tmp = L0
L0 = L1
L1 = tmp + L1 + step
}
}
printf("\n")
}
- Output:
Leonardo numbers (1,1,1): 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci numbers (0,1,0): 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Bash
#!/bin/bash
function leonardo_number () {
L0_value=${2:-1}
L1_value=${3:-1}
Add=${4:-1}
leonardo_numbers=($L0_value $L1_value)
for (( i = 2; i < $1; ++i))
do
leonardo_numbers+=( $((leonardo_numbers[i-1] + leonardo_numbers[i-2] + Add)) )
done
echo "${leonardo_numbers[*]}"
}
- Output:
leonardo_number 25 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 leonardo_number 25 0 1 0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
BASIC
BASIC256
subroutine leonardo(L0, L1, suma, texto)
print "Numeros de " + texto + " (" + L0 + "," + L1 + "," + suma + "):"
for i = 1 to 25
if i = 1 then
print L0 + " ";
else
if i = 2 then
print L1 + " ";
else
print L0 + L1 + suma + " ";
tmp = L0
L0 = L1
L1 = tmp + L1 + suma
end if
end if
next i
print chr(10)
end subroutine
#--- Programa Principal ---
call leonardo(1,1,1,"Leonardo")
call leonardo(0,1,0,"Fibonacci")
end
- Output:
Numeros de Leonardo (1,1,1): 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Numeros de Fibonacci (0,1,0): 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
IS-BASIC
100 PROGRAM "Leonardo.bas"
110 INPUT PROMPT "Enter values of L0, L1, and ADD, separated by comas: ":L0,L1,ADD
120 PRINT L0;L1;
130 FOR I=3 TO 25
140 LET T=L1:LET L1=L1+L0+ADD:LET L0=T
160 PRINT L1;
170 NEXT
180 PRINT
Sinclair ZX81 BASIC
Runs on the 1k RAM model with room to spare; hence the long(ish) variable names. The parameters are read from the keyboard.
10 INPUT L0
20 INPUT L1
30 INPUT ADD
40 PRINT L0;" ";L1;
50 FOR I=3 TO 25
60 LET TEMP=L1
70 LET L1=L0+L1+ADD
80 LET L0=TEMP
90 PRINT " ";L1;
100 NEXT I
- Input:
1 1 1
- Output:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
- Input:
0 1 0
- Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
BBC BASIC
It's a shame when fonts don't make much of a distinction between l lower-case L and 1 the number One.
REM >leonardo
:
PRINT "Enter values of L0, L1, and ADD, separated by commas:"
INPUT l0%, l1%, add%
PRINT l0% ' l1%
FOR i% = 3 TO 25
temp% = l1%
l1% += l0% + add%
l0% = temp%
PRINT l1%
NEXT
PRINT
END
- Output:
Enter values of L0, L1, and ADD, separated by commas: ?1, 1, 1 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
Enter values of L0, L1, and ADD, separated by commas: ?0, 1, 0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Burlesque
blsq ) 1 1 1{.+\/.+}\/+]23!CCLm]wdsh
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
blsq ) 0 1 0{.+\/.+}\/+]23!CCLm]wdsh
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
C
This implementation fulfills the task requirements which state that the first 2 terms and the step increment should be specified. Many other implementations on this page only print out the first 25 numbers.
#include<stdio.h>
void leonardo(int a,int b,int step,int num){
int i,temp;
printf("First 25 Leonardo numbers : \n");
for(i=1;i<=num;i++){
if(i==1)
printf(" %d",a);
else if(i==2)
printf(" %d",b);
else{
printf(" %d",a+b+step);
temp = a;
a = b;
b = temp+b+step;
}
}
}
int main()
{
int a,b,step;
printf("Enter first two Leonardo numbers and increment step : ");
scanf("%d%d%d",&a,&b,&step);
leonardo(a,b,step,25);
return 0;
}
Output : Normal Leonardo Series :
Enter first two Leonardo numbers and increment step : 1 1 1 First 25 Leonardo numbers : 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
Fibonacci Series :
Enter first two Leonardo numbers and increment step : 0 1 0 First 25 Leonardo numbers : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
C#
using System;
using System.Linq;
public class Program
{
public static void Main() {
Console.WriteLine(string.Join(" ", Leonardo().Take(25)));
Console.WriteLine(string.Join(" ", Leonardo(L0: 0, L1: 1, add: 0).Take(25)));
}
public static IEnumerable<int> Leonardo(int L0 = 1, int L1 = 1, int add = 1) {
while (true) {
yield return L0;
(L0, L1) = (L1, L0 + L1 + add);
}
}
}
- Output:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
C++
#include <iostream>
void leoN( int cnt, int l0 = 1, int l1 = 1, int add = 1 ) {
int t;
for( int i = 0; i < cnt; i++ ) {
std::cout << l0 << " ";
t = l0 + l1 + add; l0 = l1; l1 = t;
}
}
int main( int argc, char* argv[] ) {
std::cout << "Leonardo Numbers: "; leoN( 25 );
std::cout << "\n\nFibonacci Numbers: "; leoN( 25, 0, 1, 0 );
return 0;
}
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Common Lisp
;;;
;;; leo - calculates the first n number from a leo sequence.
;;; The first argument n is the number of values to return. The next three arguments a, b, add are optional.
;;; Default values provide the "original" leonardo numbers as defined in the task.
;;; a and b are the first and second element of the leonardo sequence.
;;; add is the "add number" as defined in the task definition.
;;;
(defun leo (n &optional (a 1) (b 1) (add 1))
(labels ((iterate (n foo)
(if (zerop n) (reverse foo)
(iterate (- n 1)
(cons (+ (first foo) (second foo) add) foo)))))
(cond ((= n 1) (list a))
(T (iterate (- n 2) (list b a))))))
- Output:
> (leo 25) (1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) > (leo 25 0 1 0) (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)
Crystal
def leonardo(l_zero, l_one, add, amount)
terms = [l_zero, l_one]
while terms.size < amount
new = terms[-1] + terms[-2]
new += add
terms << new
end
terms
end
puts "First 25 Leonardo numbers: \n#{ leonardo(1,1,1,25) }"
puts "Leonardo numbers with fibonacci parameters:\n#{ leonardo(0,1,0,25) }"
- Output:
First 25 Leonardo numbers: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] Leonardo numbers with fibonacci parameters: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
D
import std.stdio;
void main() {
write("Leonardo Numbers: ");
leonardoNumbers( 25 );
write("Fibonacci Numbers: ");
leonardoNumbers( 25, 0, 1, 0 );
}
void leonardoNumbers(int count, int l0=1, int l1=1, int add=1) {
int t;
for (int i=0; i<count; ++i) {
write(l0, " ");
t = l0 + l1 + add;
l0 = l1;
l1 = t;
}
writeln();
}
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Delphi
type TIntArray = array of integer;
function GetLeonardoNumbers(Cnt,SN1,SN2,Add: integer): TIntArray;
var N: integer;
begin
SetLength(Result,Cnt);
Result[0]:=SN1; Result[1]:=SN2;
for N:=2 to Cnt-1 do
begin
Result[N]:=Result[N-1] + Result[N-2] + Add;
end;
end;
procedure TestLeonardoNumbers(Memo: TMemo);
var IA: TIntArray;
var S: string;
var I: integer;
begin
Memo.Lines.Add('Leonardo Numbers:');
IA:=GetLeonardoNumbers(25,1,1,1);
S:='';
for I:=0 to High(IA) do
begin
S:=S+' '+Format('%6d',[IA[I]]);
if I mod 5 = 4 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Fibonacci Numbers:');
IA:=GetLeonardoNumbers(25,0,1,0);
S:='';
for I:=0 to High(IA) do
begin
S:=S+' '+Format('%6d',[IA[I]]);
if I mod 5 = 4 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
EasyLang
proc leonardo L0 L1 add . .
print "L0:" & L0 & " L1:" & L1 & " add:" & add
write L0 & " "
write L1 & " "
for i = 2 to 24
tmp = L0
L0 = L1
L1 = tmp + L1 + add
write L1 & " "
.
print ""
.
leonardo 1 1 1
leonardo 0 1 0
EMal
fun leonardo = List by int n, int leo0, int leo1, int add
List leo = int[].with(n)
leo[0] = leo0
leo[1] = leo1
for int i = 2; i < n; ++i
leo[i] = leo[i - 1] + leo[i - 2] + add
end
return leo
end
writeLine("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:")
writeLine(leonardo(25, 1, 1, 1))
writeLine()
writeLine("The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:")
writeLine(leonardo(25, 0, 1, 0))
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: [1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049] The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]
F#
open System
let leo l0 l1 d =
Seq.unfold (fun (x, y) -> Some (x, (y, x + y + d))) (l0, l1)
let leonardo = leo 1 1 1
let fibonacci = leo 0 1 0
[<EntryPoint>]
let main _ =
let leoNums = Seq.take 25 leonardo |> Seq.chunkBySize 16
printfn "First 25 of the (1, 1, 1) Leonardo numbers:\n%A" leoNums
Console.WriteLine()
let fibNums = Seq.take 25 fibonacci |> Seq.chunkBySize 16
printfn "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number):\n%A" fibNums
0 // return an integer exit code
- Output:
First 25 of the (1, 1, 1) Leonardo numbers: seq [[|1; 1; 3; 5; 9; 15; 25; 41; 67; 109; 177; 287; 465; 753; 1219; 1973|]; [|3193; 5167; 8361; 13529; 21891; 35421; 57313; 92735; 150049|]] First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number): seq [[|0; 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610|]; [|987; 1597; 2584; 4181; 6765; 10946; 17711; 28657; 46368|]]
Factor
USING: fry io kernel math prettyprint sequences ;
IN: rosetta-code.leonardo-numbers
: first25-leonardo ( vector add -- seq )
23 swap '[ dup 2 tail* sum _ + over push ] times ;
: print-leo ( seq -- ) [ pprint bl ] each nl ;
"First 25 Leonardo numbers:" print
V{ 1 1 } 1 first25-leonardo print-leo
"First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0:" print
V{ 0 1 } 0 first25-leonardo print-leo
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Fermat
Func Leonardo(size, l0, l1, add) =
Array leo[1,size]; {set up as a row rather than column vector; looks nicer to print}
leo[1,1]:=l0; leo[1,2]:=l1; {fermat arrays are 1-indexed}
for i=3 to size do
leo[1,i]:=leo[1,i-2]+leo[1,i-1]+add;
od;
.;
Leonardo(25, 1, 1, 1);
[leo];
Leonardo(25, 0, 1, 0);
[leo];
- Output:
[[ 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 ]] [[[ 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 ]]
Fortran
Happily, no monster values result for the trial run, so ordinary 32-bit integers suffice. The source style uses the F90 facilities only to name the subroutine being ended (i.e. END SUBROUTINE LEONARDO
rather than just END
) and the I0 format code that shows an integer without a fixed space allowance, convenient in produced well-formed messages. The "$" format code signifies that the end of output from its WRITE statement should not trigger the starting of a new line for the next WRITE statement, convenient when rolling a sequence of values to a line of output one-by-one as they are concocted. Otherwise, the values would have to be accumulated in a suitable array and then written in one go.
Many versions of Fortran have enabled parameters to be optionally supplied and F90 has standardised a protocol, also introducing a declaration syntax that can specify multiple attributes in one statement which in this case would be INTEGER, OPTIONAL:: AF
rather than two statements concerning AF. However, in a test run with CALL LEONARDO(25,1,1)
the Compaq F90/95 compiler rejected this attempt because there was another invocation with four parameters, not three, in the same program unit. By adding the rigmarole for declaring a MODULE containing the subroutine LEONARDO, its worries would be assuaged. Many compilers (and linkers, for separately-compiled routines) would check neither the number nor the type of parameters so no such complaint would be made - but when run, the code might produce wrong results or crash.
The method relies on producing a sequence of values, rather than calculating L(n) from the start each time a value from the sequence is required.
SUBROUTINE LEONARDO(LAST,L0,L1,AF) !Show the first LAST values of the sequence.
INTEGER LAST !Limit to show.
INTEGER L0,L1 !Starting values.
INTEGER AF !The "Add factor" to deviate from Fibonacci numbers.
OPTIONAL AF !Indicate that this parameter may be omitted.
INTEGER EMBOLISM !The bloat to employ.
INTEGER N,LN,LNL1,LNL2 !Assistants to the calculation.
IF (PRESENT(AF)) THEN !Perhaps the last parameter has not been given.
EMBOLISM = AF !It has. Take its value.
ELSE !But if not,
EMBOLISM = 1 !This is the specified default.
END IF !Perhaps there should be some report on this?
WRITE (6,1) LAST,L0,L1,EMBOLISM !Announce.
1 FORMAT ("The first ",I0, !The I0 format code avoids excessive spacing.
1 " numbers in the Leonardo sequence defined by L(0) = ",I0,
2 " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
IF (LAST .GE. 1) WRITE (6,2) L0 !In principle, LAST may be small.
IF (LAST .GE. 2) WRITE (6,2) L1 !!So, suspicion rules.
2 FORMAT (I0,", ",$) !Obviously, the $ sez "don't finish the line".
LNL1 = L0 !Syncopation for the sequence's initial values.
LN = L1 !Since the parameters ought not be damaged.
DO N = 3,LAST !Step away.
LNL2 = LNL1 !Advance the two state variables one step.
LNL1 = LN !Ready to make a step forward.
LN = LNL1 + LNL2 + EMBOLISM !Thus.
WRITE (6,2) LN !Reveal the value. Overflow is distant...
END DO !On to the next step.
WRITE (6,*) !Finish the line.
END SUBROUTINE LEONARDO !Only speedy for the sequential production of values.
PROGRAM POKE
CALL LEONARDO(25,1,1,1) !The first 25 Leonardo numbers.
CALL LEONARDO(25,0,1,0) !Deviates to give the Fibonacci sequence.
END
Output:
The first 25 numbers in the Leonardo sequence defined by L(0) = 1 and L(1) = 1 with L(n) = L(n - 1) + L(n - 2) + 1 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049, The first 25 numbers in the Leonardo sequence defined by L(0) = 0 and L(1) = 1 with L(n) = L(n - 1) + L(n - 2) + 0 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368,
FreeBASIC
Sub leonardo(L0 As Integer, L1 As Integer, suma As Integer, texto As String)
Dim As Integer i, tmp
Print "Numeros de " &texto &" (" &L0 &"," &L1 &"," &suma &"):"
For i = 1 To 25
If i = 1 Then
Print L0;
Elseif i = 2 Then
Print L1;
Else
Print L0 + L1 + suma;
tmp = L0
L0 = L1
L1 = tmp + L1 + suma
End If
Next i
Print Chr(10)
End Sub
'--- Programa Principal ---
leonardo(1,1,1,"Leonardo")
leonardo(0,1,0,"Fibonacci")
End
- Output:
Numeros de Leonardo (1,1,1): 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Numeros de Fibonacci (0,1,0): 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
FutureBasic
local fn LeoFiboNumbers( number as long, l0 as long, l1 as long, sum as long ) as CFArrayRef
long i, tmp
CFMutableArrayRef mutArr = fn MutableArrayWithCapacity(0)
for i = 1 to number
if i = 1
MutableArrayAddObject( mutArr, fn StringWithFormat( @"%ld", l0 ) )
else
if i = 2
MutableArrayAddObject( mutArr, fn StringWithFormat( @"%ld", l1 ) )
else
MutableArrayAddObject( mutArr, fn StringWithFormat( @"%ld", l0 + l1 + sum ) )
tmp = L0 : l0 = l1 : l1 = tmp + l1 + sum
end if
end if
next
end fn = mutArr
void local fn CompareResults( number as long )
CFArrayRef leonardoArr = fn LeoFiboNumbers( number, 1, 1, 1 )
CFArrayRef fibonacciArr = fn LeoFiboNumbers( number, 0, 1, 0 )
long i, count = fn ArrayCount( leonardoArr )
printf @"First %ld numbers of:\n%8s%11s", number, fn StringUTF8String( @"Leonardo" ), fn StringUTF8String( @"Fibonacci" )
for i = 0 to count - 1
printf @"%8s%11s", fn StringUTF8String( leonardoArr[i] ), fn StringUTF8String( fibonacciArr[i] )
next
end fn
fn CompareResults( 35 )
HandleEvents
- Output:
First 35 numbers of: Leonardo Fibonacci 1 0 1 1 3 1 5 2 9 3 15 5 25 8 41 13 67 21 109 34 177 55 287 89 465 144 753 233 1219 377 1973 610 3193 987 5167 1597 8361 2584 13529 4181 21891 6765 35421 10946 57313 17711 92735 28657 150049 46368 242785 75025 392835 121393 635621 196418 1028457 317811 1664079 514229 2692537 832040 4356617 1346269 7049155 2178309 11405773 3524578 18454929 5702887
Go
package main
import "fmt"
func leonardo(n, l0, l1, add int) []int {
leo := make([]int, n)
leo[0] = l0
leo[1] = l1
for i := 2; i < n; i++ {
leo[i] = leo[i - 1] + leo[i - 2] + add
}
return leo
}
func main() {
fmt.Println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:")
fmt.Println(leonardo(25, 1, 1, 1))
fmt.Println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:")
fmt.Println(leonardo(25, 0, 1, 0))
}
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: [1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049] The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: [0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368]
Haskell
import Data.List (intercalate, unfoldr)
import Data.List.Split (chunksOf)
--------------------- LEONARDO NUMBERS ---------------------
-- L0 -> L1 -> Add number -> Series (infinite)
leo :: Integer -> Integer -> Integer -> [Integer]
leo l0 l1 d = unfoldr (\(x, y) -> Just (x, (y, x + y + d))) (l0, l1)
leonardo :: [Integer]
leonardo = leo 1 1 1
fibonacci :: [Integer]
fibonacci = leo 0 1 0
--------------------------- TEST ---------------------------
main :: IO ()
main =
(putStrLn . unlines)
[ "First 25 default (1, 1, 1) Leonardo numbers:\n"
, f $ take 25 leonardo
, "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers):\n"
, f $ take 25 fibonacci
]
where
f = unlines . fmap (('\t' :) . intercalate ",") . chunksOf 16 . fmap show
- Output:
First 25 default (1, 1, 1) Leonardo numbers: 1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973 3193,5167,8361,13529,21891,35421,57313,92735,150049 First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers): 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 987,1597,2584,4181,6765,10946,17711,28657,46368
Alternately, defining the list self-referentially instead of using unfoldr:
leo :: Integer -> Integer -> Integer -> [Integer]
leo l0 l1 d = s where
s = l0 : l1 : zipWith (\x y -> x + y + d) s (tail s)
J
leo =: (] , {.@[ + _2&{@] + {:@])^:(_2&+@{:@[)
- Output:
1 25 leo 1 1 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 0 25 leo 0 1 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Java
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("SameParameterValue")
public class LeonardoNumbers {
private static List<Integer> leonardo(int n) {
return leonardo(n, 1, 1, 1);
}
private static List<Integer> leonardo(int n, int l0, int l1, int add) {
Integer[] leo = new Integer[n];
leo[0] = l0;
leo[1] = l1;
for (int i = 2; i < n; i++) {
leo[i] = leo[i - 1] + leo[i - 2] + add;
}
return Arrays.asList(leo);
}
public static void main(String[] args) {
System.out.println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:");
System.out.println(leonardo(25));
System.out.println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:");
System.out.println(leonardo(25, 0, 1, 0));
}
}
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
JavaScript
ES6
const leoNum = (c, l0 = 1, l1 = 1, add = 1) =>
new Array(c).fill(add).reduce(
(p, c, i) => i > 1 ? (
p.push(p[i - 1] + p[i - 2] + c) && p
) : p, [l0, l1]
);
console.log(leoNum(25));
console.log(leoNum(25, 0, 1, 0));
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Or, taking N terms from a non-finite Javascript generator:
(() => {
'use strict';
// leo :: Int -> Int -> Int -> Generator [Int]
function* leo(L0, L1, delta) {
let [x, y] = [L0, L1];
while (true) {
yield x;
[x, y] = [y, delta + x + y];
}
}
// ----------------------- TEST ------------------------
// main :: IO ()
const main = () => {
const
leonardo = leo(1, 1, 1),
fibonacci = leo(0, 1, 0);
return unlines([
'First 25 Leonardo numbers:',
indentWrapped(take(25)(leonardo)),
'',
'First 25 Fibonacci numbers:',
indentWrapped(take(25)(fibonacci))
]);
};
// -------------------- FORMATTING ---------------------
// indentWrapped :: [Int] -> String
const indentWrapped = xs =>
unlines(
map(x => '\t' + x.join(','))(
chunksOf(16)(
map(str)(xs)
)
)
);
// ----------------- GENERIC FUNCTIONS -----------------
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n =>
xs => enumFromThenTo(0)(n)(
xs.length - 1
).reduce(
(a, i) => a.concat([xs.slice(i, (n + i))]),
[]
);
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = x1 =>
x2 => y => {
const d = x2 - x1;
return Array.from({
length: Math.floor(y - x2) / d + 2
}, (_, i) => x1 + (d * i));
};
// map :: (a -> b) -> [a] -> [b]
const map = f =>
// The list obtained by applying f
// to each element of xs.
// (The image of xs under f).
xs => [...xs].map(f);
// str :: a -> String
const str = x =>
x.toString();
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = n =>
// The first n elements of a list,
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}));
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// MAIN ---
return main();
})();
- Output:
First 25 Leonardo numbers: 1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973 3193,5167,8361,13529,21891,35421,57313,92735,150049 First 25 Fibonacci numbers: 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 987,1597,2584,4181,6765,10946,17711,28657,46368
jq
Naive Implementation
def Leonardo(zero; one; incr):
def leo:
if . == 0 then zero
elif . == 1 then one
else ((.-1) |leo) + ((.-2) | leo) + incr
end;
leo;
Implementation with Caching
An array is used for caching, with `.[n]` storing the value L(n).
def Leonardo(zero; one; incr):
def leo(n):
if .[n] then .
else leo(n-1) # optimization of leo(n-2)|leo(n-1)
| .[n] = .[n-1] + .[n-2] + incr
end;
. as $n | [zero,one] | leo($n) | .[$n];
(To compute the sequence of Leonardo numbers L(1) ... L(n) without redundant computation, the last element of the pipeline in the last line of the function above should be dropped.)
Examples
[range(0;25) | Leonardo(1;1;1)]
- Output:
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049]
[range(0;25) | Leonardo(0;1;0)]
- Output:
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]
Julia
function L(n, add::Int=1, firsts::Vector=[1, 1])
l = max(maximum(n) .+ 1, length(firsts))
r = Vector{Int}(l)
r[1:length(firsts)] = firsts
for i in 3:l
r[i] = r[i - 1] + r[i - 2] + add
end
return r[n .+ 1]
end
# Task 1
println("First 25 Leonardo numbers: ", join(L(0:24), ", "))
# Task 2
@show L(0) L(1)
# Task 4
println("First 25 Leonardo numbers starting with [0, 1]: ", join(L(0:24, 0, [0, 1]), ", "))
- Output:
First 25 Leonardo numbers: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 L(0) = 1 L(1) = 1 First 25 Leonardo numbers starting with 0, 1: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368
Kotlin
// version 1.1.2
fun leonardo(n: Int, l0: Int = 1, l1: Int = 1, add: Int = 1): IntArray {
val leo = IntArray(n)
leo[0] = l0
leo[1] = l1
for (i in 2 until n) leo[i] = leo[i - 1] + leo[i - 2] + add
return leo
}
fun main(args: Array<String>) {
println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:")
println(leonardo(25).joinToString(" "))
println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:")
println(leonardo(25, 0, 1, 0).joinToString(" "))
}
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Lua
function leoNums (n, L0, L1, add)
local L0, L1, add = L0 or 1, L1 or 1, add or 1
local lNums, nextNum = {L0, L1}
while #lNums < n do
nextNum = lNums[#lNums] + lNums[#lNums - 1] + add
table.insert(lNums, nextNum)
end
return lNums
end
function show (msg, t)
print(msg .. ":")
for i, x in ipairs(t) do
io.write(x .. " ")
end
print("\n")
end
show("Leonardo numbers", leoNums(25))
show("Fibonacci numbers", leoNums(25, 0, 1, 0))
- Output:
Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Maple
L := proc(n, L_0, L_1, add)
if n = 0 then
return L_0;
elif n = 1 then
return L_1;
else
return L(n - 1) + L(n - 2) + add;
end if;
end proc:
Leonardo := n -> (L(1, 1, 1),[seq(0..n - 1)])
Fibonacci := n -> (L(0, 1, 0), [seq(0..n - 1)])
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Mathematica/Wolfram Language
L[0,L0_:1,___]:=L0
L[1,L0_:1,L1_:1,___]:=L1
L[n_,L0_:1,L1_:1,add_:1]:=L[n-1,L0,L1,add]+L[n-2,L0,L1,add]+add
L/@(Range[25]-1)
L[#,0,1,0]&/@(Range[25]-1)
{1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049} {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368}
Maxima
/* Function that return the terms of an specified Leonardo sequence */
leo_specified(n,L0,L1,add):=block(
if n=0 then L[n]:L0 else if n=1 then L[n]:L1 else L[n]:L[n-1]+L[n-2]+add,
L[n])$
/* Test cases */
/* First 25 terms of Leonardo numbers (specification (1,1,1)) */
makelist(leo_specified(i,1,1,1),i,0,25);
/* First 25 terms of Fibonacci numbers (specification (0,1,0)) */
makelist(leo_specified(i,0,1,0),i,0,25);
- Output:
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049,242785] [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025]
min
(over over + rolldown pop pick +) :next
(('print dip " " print! next) 25 times newline) :leo
"First 25 Leonardo numbers:" puts!
1 1 1 leo
"First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1:" puts!
0 0 1 leo
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Modula-2
MODULE Leonardo;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
PROCEDURE leonardo(a,b,step,num : INTEGER);
VAR
buf : ARRAY[0..63] OF CHAR;
i,temp : INTEGER;
BEGIN
FOR i:=1 TO num DO
IF i=1 THEN
FormatString(" %i", buf, a);
WriteString(buf)
ELSIF i=2 THEN
FormatString(" %i", buf, b);
WriteString(buf)
ELSE
FormatString(" %i", buf, a+b+step);
WriteString(buf);
temp := a;
a := b;
b := temp + b + step
END
END;
WriteLn
END leonardo;
BEGIN
leonardo(1,1,1,25);
leonardo(0,1,0,25);
ReadChar
END Leonardo.
Nim
import strformat
proc leonardoNumbers(count: int, L0: int = 1,
L1: int = 1, ADD: int = 1) =
var t = 0
var (L0_loc, L1_loc) = (L0, L1)
for i in 0..<count:
write(stdout, fmt"{L0_loc:7}")
t = L0_loc + L1_loc + ADD
L0_loc = L1_loc
L1_loc = t
if i mod 5 == 4:
write(stdout, "\n")
write(stdout, "\n")
echo "Leonardo Numbers:"
leonardoNumbers(25)
echo "Fibonacci Numbers: "
leonardoNumbers(25, 0, 1, 0)
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
OCaml
let seq_leonardo i =
let rec next b a () = Seq.Cons (a, next (a + b + i) b) in
next
let () =
let show (s, a, b, i) =
seq_leonardo i b a |> Seq.take 25
|> Seq.fold_left (Printf.sprintf "%s %u") (Printf.sprintf "First 25 %s numbers:\n" s)
|> print_endline
in
List.iter show ["Leonardo", 1, 1, 1; "Fibonacci", 0, 1, 0]
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Perl
no warnings 'experimental::signatures';
use feature 'signatures';
sub leonardo ($n, $l0 = 1, $l1 = 1, $add = 1) {
($l0, $l1) = ($l1, $l0+$l1+$add) for 1..$n;
$l0;
}
my @L = map { leonardo($_) } 0..24;
print "Leonardo[1,1,1]: @L\n";
my @F = map { leonardo($_,0,1,0) } 0..24;
print "Leonardo[0,1,0]: @F\n";
- Output:
Leonardo[1,1,1]: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Leonardo[0,1,0]: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Odin
package main
/* imports */
import "core:fmt"
/* main */
main :: proc() {
fmt.println("\nThe first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:")
result := leonardo(25, 1, 1, 1)
fmt.println(result)
delete(result)
fmt.println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:")
result = leonardo(25, 0, 1, 0)
fmt.println(result)
delete(result)
}
/* definitions */
leonardo :: proc(n, l0, l1, add: int) -> []int {
leo := make([]int, n)
leo[0] = l0
leo[1] = l1
for i in 2 ..< n {
leo[i] = leo[i - 1] + leo[i - 2] + add
}
return leo
}
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Phix
with javascript_semantics function leonardo(integer n, l1=1, l2=1, step=1) -- return the first n leonardo numbers, starting {l1,l2}, with step as the add number sequence res = {l1,l2} while length(res)<n do res = append(res,res[$]+res[$-1]+step) end while return res end function ?{"Leonardo",leonardo(25)} ?{"Fibonacci",leonardo(25,0,1,0)}
- Output:
{"Leonardo",{1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049}} {"Fibonacci",{0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368}}
Picat
go =>
println([leonardo(I) : I in 0..24]),
println([leonardo(0,1,0,I) : I in 0..24]).
leonardo(N) = leonardo(1,1,1,N).
table
leonardo(I1,_I2,_Add,0) = I1.
leonardo(_I1,I2,_Add,1) = I2.
leonardo(I1,I2,Add,N) = leonardo(I1,I2,Add,N-1) + leonardo(I1,I2,Add,N-2) + Add.
- Output:
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049] [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]
PicoLisp
(de leo (A B C)
(default A 1 B 1 C 1)
(make
(do 25
(inc
'B
(+ (link (swap 'A B)) C) ) ) ) )
(println 'Leonardo (leo))
(println 'Fibonacci (leo 0 1 0))
- Output:
Leonardo (1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) Fibonacci (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)
Plain English
To run:
Start up.
Write "First 25 Leonardo numbers:" on the console.
Show 25 of the Leonardo numbers starting with 1 and 1 and using 1 for the add number.
Write "First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0:" on the console.
Show 25 of the Leonardo numbers starting with 0 and 1 and using 0 for the add number.
Wait for the escape key.
Shut down.
To show a number of the Leonardo numbers starting with a first number and a second number and using an add number for the add number:
If the number is less than 2, exit.
Privatize the number.
Privatize the first number.
Privatize the second number.
Subtract 2 from the number.
Write the first number then " " then the second number on the console without advancing.
Loop.
If a counter is past the number, write "" on the console; exit.
Swap the first number with the second number.
Put the first number plus the second number plus the add number into the second number.
Write the second number then " " on the console without advancing.
Repeat.
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
PureBasic
EnableExplicit
#N = 25
Procedure leon_R(a.i, b.i, s.i = 1, n.i = #N)
If n>2
Print(Space(1) + Str(a + b + s))
ProcedureReturn leon_R(b, a + b + s, s, n-1)
EndIf
EndProcedure
If OpenConsole()
Define r$
Print("Enter first two Leonardo numbers and increment step (separated by space) : ")
r$ = Input()
PrintN("First " + Str(#N) + " Leonardo numbers : ")
Print(StringField(r$, 1, Chr(32)) + Space(1) +
StringField(r$, 2, Chr(32)))
leon_R(Val(StringField(r$, 1, Chr(32))),
Val(StringField(r$, 2, Chr(32))),
Val(StringField(r$, 3, Chr(32))))
r$ = Input()
EndIf
- Output:
Enter first two Leonardo numbers and increment step (separated by space) : 1 1 1 First 25 Leonardo numbers : 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Enter first two Leonardo numbers and increment step (separated by space) : 0 1 0 First 25 Leonardo numbers : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Python
Finite iteration
def Leonardo(L_Zero, L_One, Add, Amount):
terms = [L_Zero,L_One]
while len(terms) < Amount:
new = terms[-1] + terms[-2]
new += Add
terms.append(new)
return terms
out = ""
print "First 25 Leonardo numbers:"
for term in Leonardo(1,1,1,25):
out += str(term) + " "
print out
out = ""
print "Leonardo numbers with fibonacci parameters:"
for term in Leonardo(0,1,0,25):
out += str(term) + " "
print out
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Leonardo numbers with fibonacci parameters: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Non-finite generation
Or, for a non-finite stream of Leonardos, we can use a Python generator:
'''Leonardo numbers'''
from functools import (reduce)
from itertools import (islice)
# leo :: Int -> Int -> Int -> Generator [Int]
def leo(L0, L1, delta):
'''A number series of the
Leonardo and Fibonacci pattern,
where L0 and L1 are the first two terms,
and delta = 1 for (L0, L1) == (1, 1)
yields the Leonardo series, while
delta = 0 defines the Fibonacci series.'''
(x, y) = (L0, L1)
while True:
yield x
(x, y) = (y, x + y + delta)
# main :: IO()
def main():
'''Tests.'''
print('\n'.join([
'First 25 Leonardo numbers:',
folded(16)(take(25)(
leo(1, 1, 1)
)),
'',
'First 25 Fibonacci numbers:',
folded(16)(take(25)(
leo(0, 1, 0)
))
]))
# FORMATTING ----------------------------------------------
# folded :: Int -> [a] -> String
def folded(n):
'''Long list folded to rows of n terms each.'''
return lambda xs: '[' + ('\n '.join(
str(ns)[1:-1] for ns in chunksOf(n)(xs)
) + ']')
# GENERIC -------------------------------------------------
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n,
subdividing the contents of xs.
Where the length of xs is not evenly divible,
the final list will be shorter than n.'''
return lambda xs: reduce(
lambda a, i: a + [xs[i:n + i]],
range(0, len(xs), n), []
) if 0 < n else []
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.'''
return lambda xs: (
xs[0:n]
if isinstance(xs, list)
else list(islice(xs, n))
)
# MAIN ---
if __name__ == '__main__':
main()
- Output:
First 25 Leonardo numbers: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] First 25 Fibonacci numbers: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Quackery
[ 1 1 1 ] is leo ( --> n n n )
[ 0 1 0 ] is fibo ( --> n n n )
[ 2 1 0 ] is lucaso ( --> n n n )
[ temp put
rot times
[ tuck +
temp share + ]
temp release drop ] is nardo ( n n n n --> n )
say "Leonardo numbers:" cr
25 times [ i^ leo nardo echo sp ]
cr cr
say "Fibonacci numbers:" cr
25 times [ i^ fibo nardo echo sp ]
cr cr
say "Lucas numbers:" cr
25 times [ i^ lucaso nardo echo sp ]
- Output:
Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 Lucas numbers: 2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 1364 2207 3571 5778 9349 15127 24476 39603 64079 103682
R
leonardo_numbers <- function(add = 1, l0 = 1, l1 = 1, how_many = 25) {
result <- c(l0, l1)
for (i in 3:how_many)
result <- append(result, result[[i - 1]] + result[[i - 2]] + add)
result
}
cat("First 25 Leonardo numbers\n")
cat(leonardo_numbers(), "\n")
cat("First 25 Leonardo numbers from 0, 1 with add number = 0\n")
cat(leonardo_numbers(0, 0, 1), "\n")
- Output:
First 25 Leonardo numbers 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers from 0, 1 with add number = 0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Racket
#lang racket
(define (Leonardo n #:L0 (L0 1) #:L1 (L1 1) #:1+ (1+ 1))
(cond [(= n 0) L0]
[(= n 1) L1]
[else
(let inr ((n (- n 2)) (L_n-2 L0) (L_n-1 L1))
(let ((L_n (+ L_n-1 L_n-2 1+)))
(if (zero? n) L_n (inr (sub1 n) L_n-1 L_n))))]))
(module+ main
(map Leonardo (range 25))
(map (curry Leonardo #:L0 0 #:L1 1 #:1+ 0) (range 25)))
(module+ test
(require rackunit)
(check-equal? (Leonardo 0) 1)
(check-equal? (Leonardo 1) 1)
(check-equal? (Leonardo 2) 3)
(check-equal? (Leonardo 3) 5))
- Output:
'(1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) '(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)
Raku
(formerly Perl 6)
sub 𝑳 ( $𝑳0 = 1, $𝑳1 = 1, $𝑳add = 1 ) { $𝑳0, $𝑳1, { $^n2 + $^n1 + $𝑳add } ... * }
# Part 1
say "The first 25 Leonardo numbers:";
put 𝑳()[^25];
# Part 2
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:";
put 𝑳( 0, 1, 0 )[^25];
- Output:
The first 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
REXX
/*REXX pgm computes Leonardo numbers, allowing the specification of L(0), L(1), and ADD#*/
numeric digits 500 /*just in case the user gets ka-razy. */
@.=1 /*define the default for the @. array.*/
parse arg N L0 L1 a# . /*obtain optional arguments from the CL*/
if N =='' | N =="," then N= 25 /*Not specified? Then use the default.*/
if L0\=='' & L0\=="," then @.0= L0 /*Was " " " " value. */
if L1\=='' & L1\=="," then @.1= L1 /* " " " " " " */
if a#\=='' & a#\=="," then @.a= a# /* " " " " " " */
say 'The first ' N " Leonardo numbers are:" /*display a title for the output series*/
if @.0\==1 | @.1\==1 then say 'using ' @.0 " for L(0)"
if @.0\==1 | @.1\==1 then say 'using ' @.1 " for L(1)"
if @.a\==1 then say 'using ' @.a " for the add number"
say /*display blank line before the output.*/
$= /*initialize the output line to "null".*/
do j=0 for N /*construct a list of Leonardo numbers.*/
if j<2 then z=@.j /*for the 1st two numbers, use the fiat*/
else do /*··· otherwise, compute the Leonardo #*/
_=@.0 /*save the old primary Leonardo number.*/
@.0=@.1 /*store the new primary number in old. */
@.1=@.0 + _ + @.a /*compute the next Leonardo number. */
z=@.1 /*store the next Leonardo number in Z. */
end /* [↑] only 2 Leonardo #s are stored. */
$=$ z /*append the just computed # to $ list.*/
end /*j*/ /* [↓] elide the leading blank in $. */
say strip($) /*stick a fork in it, we're all done. */
- output when using the default input:
The first 25 Leonardo numbers are: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
- output when using the input of: 12 0 1 0
The first 25 Leonardo numbers are: using 0 for L(0) using 1 for L(1) using 0 for the add number 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Ring
# Project : Leanardo numbers
n0 = 1
n1 = 1
add = 1
see "First 25 Leonardo numbers:" + nl
leonardo()
n0 = 1
n1 = 1
add = 0
see "First 25 Leonardo numbers with L(0) = 0, L(1) = 1, step = 0 (fibonacci numbers):" + nl
see "" + add + " "
leonardo()
func leonardo()
see "" + n0 + " " + n1
for i=3 to 25
temp=n1
n1=n0+n1+add
n0=temp
see " "+ n1
next
see nl
Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers with L(0) = 0, L(1) = 1, step = 0 (fibonacci numbers): 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025
RPL
Code | Comments |
---|---|
≪ → l0 l1 add n ≪ l0 l1 2 →LIST IF n 3 ≥ THEN l0 l1 2 n 1 - START DUP ROT + add + ROT OVER + 3 ROLLD NEXT DROP2 END ≫ ≫ 'LENDO' STO |
( L(0) L(1) add n -- { L(0) .. L(n-1) } ) Initialize sequence Initialise stack and loop Calculate next L(i) Store it into sequence list Clean stack |
- Input:
1 1 1 25 '''LENDO''' 0 1 0 25 '''LENDO'''
- Output:
2: { 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 } 1: { 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 }
One-liner
≪ 3 MAX → l0 l1 add n ≪ l0 l1 2 n START DUP2 + add + NEXT n 1 + →LIST ≫ ≫ 'LEONE' STO
- Input:
1,1,1,24 '''LEONE''' 0,1,0,24 '''LEONE'''
Same output as above.
Ruby
Enumerators are nice for this.
def leonardo(l0=1, l1=1, add=1)
return to_enum(__method__,l0,l1,add) unless block_given?
loop do
yield l0
l0, l1 = l1, l0+l1+add
end
end
p leonardo.take(25)
p leonardo(0,1,0).take(25)
- Output:
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Run BASIC
sqliteconnect #mem, ":memory:"
#mem execute("CREATE TABLE lno (name,L0,L1,ad)")
#mem execute("INSERT INTO lno VALUES('Leonardo',1,1,1),('Fibonacci',0,1,0);")
#mem execute("SELECT * FROM lno")
for j = 1 to 2
#row = #mem #nextrow()
name$ = #row name$()
L0 = #row L0()
L1 = #row L1()
ad = #row ad()
print :print name$;" add=";ad :print" ";L0;" ";L1;" ";
for i = 3 to 25
temp = L1
L1 = L0 + L1 + ad
L0 = temp
print L1;" ";
next i
next j
end
Leonardo add=1 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci add=0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Rust
fn leonardo(mut n0: u32, mut n1: u32, add: u32) -> impl std::iter::Iterator<Item = u32> {
std::iter::from_fn(move || {
let n = n0;
n0 = n1;
n1 += n + add;
Some(n)
})
}
fn main() {
println!("First 25 Leonardo numbers:");
for i in leonardo(1, 1, 1).take(25) {
print!("{} ", i);
}
println!();
println!("First 25 Fibonacci numbers:");
for i in leonardo(0, 1, 0).take(25) {
print!("{} ", i);
}
println!();
}
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Scala
def leo( n:Int, n1:Int=1, n2:Int=1, addnum:Int=1 ) : BigInt = n match {
case 0 => n1
case 1 => n2
case n => leo(n - 1, n1, n2, addnum) + leo(n - 2, n1, n2, addnum) + addnum
}
{
println( "The first 25 Leonardo Numbers:")
(0 until 25) foreach { n => print( leo(n) + " " ) }
println( "\n\nThe first 25 Fibonacci Numbers:")
(0 until 25) foreach { n => print( leo(n, n1=0, n2=1, addnum=0) + " " ) }
}
- Output:
The first 25 Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Seed7
$ include "seed7_05.s7i";
const proc: leonardo (in var integer: l0, in var integer: l1, in integer: add, in integer: count) is func
local
var integer: temp is 0;
begin
for count do
write(" " <& l0);
temp := l0 + l1 + add;
l0 := l1;
l1 := temp;
end for;
writeln;
end func;
const proc: main is func
begin
write("Leonardo Numbers:");
leonardo(1, 1, 1, 25);
write("Fibonacci Numbers:");
leonardo(0, 1, 0, 25);
end func;
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Sidef
func 𝑳(n, 𝑳0 = 1, 𝑳1 = 1, 𝑳add = 1) {
{ (𝑳0, 𝑳1) = (𝑳1, 𝑳0 + 𝑳1 + 𝑳add) } * n
return 𝑳0
}
say "The first 25 Leonardo numbers:"
say 25.of { 𝑳(_) }
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:"
say 25.of { 𝑳(_, 0, 1, 0) }
- Output:
The first 25 Leonardo numbers: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Swift
struct Leonardo: Sequence, IteratorProtocol {
private let add : Int
private var n0: Int
private var n1: Int
init(n0: Int = 1, n1: Int = 1, add: Int = 1) {
self.n0 = n0
self.n1 = n1
self.add = add
}
mutating func next() -> Int? {
let n = n0
n0 = n1
n1 += n + add
return n
}
}
print("First 25 Leonardo numbers:")
print(Leonardo().prefix(25).map{String($0)}.joined(separator: " "))
print("First 25 Fibonacci numbers:")
print(Leonardo(n0: 0, add: 0).prefix(25).map{String($0)}.joined(separator: " "))
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
VBA
Option Explicit
Private Sub LeonardoNumbers()
Dim L, MyString As String
Debug.Print "First 25 Leonardo numbers :"
L = Leo_Numbers(25, 1, 1, 1)
MyString = Join(L, "; ")
Debug.Print MyString
Debug.Print "First 25 Leonardo numbers from 0, 1 with add number = 0"
L = Leo_Numbers(25, 0, 1, 0)
MyString = Join(L, "; ")
Debug.Print MyString
Debug.Print "If the first prarameter is too small :"
L = Leo_Numbers(1, 0, 1, 0)
MyString = Join(L, "; ")
Debug.Print MyString
End Sub
Public Function Leo_Numbers(HowMany As Long, L_0 As Long, L_1 As Long, Add_Nb As Long)
Dim N As Long, Ltemp
If HowMany > 1 Then
ReDim Ltemp(HowMany - 1)
Ltemp(0) = L_0: Ltemp(1) = L_1
For N = 2 To HowMany - 1
Ltemp(N) = Ltemp(N - 1) + Ltemp(N - 2) + Add_Nb
Next N
Else
ReDim Ltemp(0)
Ltemp(0) = "The first parameter is too small"
End If
Leo_Numbers = Ltemp
End Function
- Output:
First 25 Leonardo numbers : 1; 1; 3; 5; 9; 15; 25; 41; 67; 109; 177; 287; 465; 753; 1219; 1973; 3193; 5167; 8361; 13529; 21891; 35421; 57313; 92735; 150049 First 25 Leonardo numbers from 0, 1 with add number = 0 0; 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610; 987; 1597; 2584; 4181; 6765; 10946; 17711; 28657; 46368 If the first prarameter is too small : The first parameter is too small
Visual Basic .NET
Module Module1
Iterator Function Leonardo(Optional L0 = 1, Optional L1 = 1, Optional add = 1) As IEnumerable(Of Integer)
While True
Yield L0
Dim t = L0 + L1 + add
L0 = L1
L1 = t
End While
End Function
Sub Main()
Console.WriteLine(String.Join(" ", Leonardo().Take(25)))
Console.WriteLine(String.Join(" ", Leonardo(0, 1, 0).Take(25)))
End Sub
End Module
- Output:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
V (Vlang)
fn leonardo(n int, l0 int, l1 int, add int) []int {
mut leo := []int{len: n}
leo[0] = l0
leo[1] = l1
for i := 2; i < n; i++ {
leo[i] = leo[i - 1] + leo[i - 2] + add
}
return leo
}
fn main() {
println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:")
println(leonardo(25, 1, 1, 1))
println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:")
println(leonardo(25, 0, 1, 0))
}
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Wren
var leonardo = Fn.new { |first, add, limit|
var leo = List.filled(limit, 0)
leo[0] = first[0]
leo[1] = first[1]
for (i in 2...limit) leo[i] = leo[i-1] + leo[i-2] + add
return leo
}
System.print("The first 25 Leonardo numbers with L(0) = 1, L(1) = 1 and Add = 1 are:")
for (l in leonardo.call([1, 1], 1, 25)) System.write("%(l) ")
System.print("\n\nThe first 25 Leonardo numbers with L(0) = 0, L(1) = 1 and Add = 0 are:")
for (l in leonardo.call([0, 1], 0, 25)) System.write("%(l) ")
System.print()
- Output:
The first 25 Leonardo numbers with L(0) = 1, L(1) = 1 and Add = 1 are: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Leonardo numbers with L(0) = 0, L(1) = 1 and Add = 0 are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Yabasic
limit = 25
sub leonardo(L0, L1, suma, texto$)
local i
print "Numeros de " + texto$, " (", L0, ",", L1, ",", suma, "):"
for i = 1 to limit
if i = 1 then print L0, " ";
elsif i = 2 then print L1, " ";
else
print L0 + L1 + suma, " ";
tmp = L0
L0 = L1
L1 = tmp + L1 + suma
endif
next i
print chr$(10)
end sub
leonardo(1,1,1,"Leonardo")
leonardo(0,1,0,"Fibonacci")
end
XPL0
int N, L, L0, L1, Add;
[Text(0, "Enter L(0), L(1), Add: ");
L0:= IntIn(0);
L1:= IntIn(0);
Add:= IntIn(0);
IntOut(0, L0); ChOut(0, ^ );
IntOut(0, L1); ChOut(0, ^ );
for N:= 3 to 25 do
[L:= L1 + L0 + Add;
IntOut(0, L); ChOut(0, ^ );
L0:= L1;
L1:= L;
];
]
- Output:
Enter L(0), L(1), Add: 1 1 1 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Enter L(0), L(1), Add: 0 1 0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
zkl
fcn leonardoNumber(n, n1=1,n2=1,addnum=1){
if(n==0) return(n1);
if(n==1) return(n2);
self.fcn(n-1,n1,n2,addnum) + self.fcn(n-2,n1,n2,addnum) + addnum
}
println("The first 25 Leonardo Numbers:");
foreach n in (25){ print(leonardoNumber(n)," ") }
println("\n");
println("The first 25 Fibonacci Numbers:");
foreach n in (25){ print(leonardoNumber(n, 0,1,0)," ") }
println();
- Output:
The first 25 Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
- Programming Tasks
- Arithmetic operations
- Classic CS problems and programs
- 11l
- Action!
- Ada
- ALGOL 68
- AppleScript
- Arturo
- AutoHotkey
- AWK
- Bash
- BASIC
- BASIC256
- IS-BASIC
- Sinclair ZX81 BASIC
- BBC BASIC
- Burlesque
- C
- C sharp
- C++
- Common Lisp
- Crystal
- D
- Delphi
- SysUtils,StdCtrls
- EasyLang
- EMal
- F Sharp
- Factor
- Fermat
- Fortran
- FreeBASIC
- FutureBasic
- Go
- Haskell
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lua
- Maple
- Mathematica
- Wolfram Language
- Maxima
- Min
- Modula-2
- Nim
- OCaml
- Perl
- Odin
- Phix
- Picat
- PicoLisp
- Plain English
- PureBasic
- Python
- Quackery
- R
- Racket
- Raku
- REXX
- Ring
- RPL
- Ruby
- Run BASIC
- Rust
- Scala
- Seed7
- Sidef
- Swift
- VBA
- Visual Basic .NET
- V (Vlang)
- Wren
- Yabasic
- XPL0
- Zkl