Leonardo numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 80: | Line 80: | ||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="11l">F leo_numbers(cnt, =l0 = 1, =l1 = 1, add = 1) |
||
L 1..cnt |
L 1..cnt |
||
print(l0, end' ‘ ’) |
print(l0, end' ‘ ’) |
||
Line 89: | Line 89: | ||
leo_numbers(25) |
leo_numbers(25) |
||
print(‘Fibonacci Numbers: ’, end' ‘’) |
print(‘Fibonacci Numbers: ’, end' ‘’) |
||
leo_numbers(25, 0, 1, 0)</ |
leo_numbers(25, 0, 1, 0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 98: | Line 98: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">CARD FUNC Leonardo(BYTE n) |
||
CARD curr,prev,tmp |
CARD curr,prev,tmp |
||
Line 132: | Line 132: | ||
PrintF("L(%B)=%U",n,l) |
PrintF("L(%B)=%U",n,l) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Leonardo_numbers.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Leonardo_numbers.png Screenshot from Atari 8-bit computer] |
||
Line 151: | Line 151: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Leonardo is |
procedure Leonardo is |
||
Line 180: | Line 180: | ||
end loop; |
end loop; |
||
New_Line; |
New_Line; |
||
end Leonardo;</ |
end Leonardo;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 190: | Line 190: | ||
=={{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 231: | 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 246: | Line 246: | ||
Drawing N items from a non-finite generator: |
Drawing N items from a non-finite generator: |
||
< |
<syntaxhighlight lang="applescript">------------------------ GENERATOR ------------------------- |
||
-- leo :: Int -> Int -> Int -> Generator [Int] |
-- leo :: Int -> Int -> Int -> Generator [Int] |
||
Line 416: | Line 416: | ||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
str |
str |
||
end unlines</ |
end unlines</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>First 25 Leonardo numbers: |
<pre>First 25 Leonardo numbers: |
||
Line 428: | Line 428: | ||
===Idiomatic=== |
===Idiomatic=== |
||
Allowing optional 'L0', 'L1', and/or 'add' specs with any version of AppleScript. |
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) |
on leonardos(spec, quantity) |
||
-- Assign the spec values to variables, using defaults for any not given. |
-- Assign the spec values to variables, using defaults for any not given. |
||
Line 454: | Line 454: | ||
" & leonardos({L0:0, L1:1, add:0}, 25) |
" & leonardos({L0:0, L1:1, add:0}, 25) |
||
set AppleScript's text item delimiters to astid |
set AppleScript's text item delimiters to astid |
||
return output</ |
return output</syntaxhighlight> |
||
{{output}} |
{{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 |
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: |
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"</ |
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}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">L: function [n l0 l1 ladd].memoize[ |
||
(n=0)? -> l0 [ |
(n=0)? -> l0 [ |
||
(n=1)? -> l1 |
(n=1)? -> l1 |
||
Line 477: | Line 477: | ||
print "" |
print "" |
||
print "The first 25 Leonardo numbers with L0=0, L1=1, LADD=0" |
print "The first 25 Leonardo numbers with L0=0, L1=1, LADD=0" |
||
print map 0..24 'x -> L x 0 1 0</ |
print map 0..24 'x -> L x 0 1 0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 488: | Line 488: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">Leonardo(n, L0:=1, L1:=1, step:=1){ |
||
if n=0 |
if n=0 |
||
return L0 |
return L0 |
||
Line 494: | Line 494: | ||
return L1 |
return L1 |
||
return Leonardo(n-1, L0, L1, step) + Leonardo(n-2, L0, L1, step) + step |
return Leonardo(n-1, L0, L1, step) + Leonardo(n-2, L0, L1, step) + step |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">output := "1st 25 Leonardo numbers, starting at L(0).`n" |
||
loop, 25 |
loop, 25 |
||
output .= Leonardo(A_Index-1) " " |
output .= Leonardo(A_Index-1) " " |
||
Line 502: | Line 502: | ||
output .= Leonardo(A_Index-1, 0, 1, 0) " " |
output .= Leonardo(A_Index-1, 0, 1, 0) " " |
||
MsgBox % output |
MsgBox % output |
||
return</ |
return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1st 25 Leonardo numbers, starting at L(0). |
<pre>1st 25 Leonardo numbers, starting at L(0). |
||
Line 511: | Line 511: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f LEONARDO_NUMBERS.AWK |
# syntax: GAWK -f LEONARDO_NUMBERS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 536: | Line 536: | ||
printf("\n") |
printf("\n") |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 546: | Line 546: | ||
=={{header|Bash}}== |
=={{header|Bash}}== |
||
<syntaxhighlight lang="bash"> |
|||
<lang BASH> |
|||
#!/bin/bash |
#!/bin/bash |
||
Line 560: | Line 560: | ||
echo "${leonardo_numbers[*]}" |
echo "${leonardo_numbers[*]}" |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 572: | Line 572: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
subroutine leonardo(L0, L1, suma, texto) |
subroutine leonardo(L0, L1, suma, texto) |
||
print "Numeros de " + texto + " (" + L0 + "," + L1 + "," + suma + "):" |
print "Numeros de " + texto + " (" + L0 + "," + L1 + "," + suma + "):" |
||
Line 596: | Line 596: | ||
call leonardo(0,1,0,"Fibonacci") |
call leonardo(0,1,0,"Fibonacci") |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 607: | Line 607: | ||
==={{header|IS-BASIC}}=== |
==={{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 |
110 INPUT PROMPT "Enter values of L0, L1, and ADD, separated by comas: ":L0,L1,ADD |
||
120 PRINT L0;L1; |
120 PRINT L0;L1; |
||
Line 614: | Line 614: | ||
160 PRINT L1; |
160 PRINT L1; |
||
170 NEXT |
170 NEXT |
||
180 PRINT</ |
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 627: | 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 643: | Line 643: | ||
=={{header|BBC BASIC}}== |
=={{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. |
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:" |
PRINT "Enter values of L0, L1, and ADD, separated by commas:" |
||
Line 655: | Line 655: | ||
NEXT |
NEXT |
||
PRINT |
PRINT |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter values of L0, L1, and ADD, separated by commas: |
<pre>Enter values of L0, L1, and ADD, separated by commas: |
||
Line 713: | Line 713: | ||
=={{header|Burlesque}}== |
=={{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 |
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 |
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</ |
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> |
|||
#include<stdio.h> |
#include<stdio.h> |
||
Line 756: | Line 756: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
Output : |
||
Normal Leonardo Series : |
Normal Leonardo Series : |
||
Line 773: | Line 773: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
{{works with|C sharp|7}} |
{{works with|C sharp|7}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Linq; |
using System.Linq; |
||
Line 789: | Line 789: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 797: | Line 797: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
Line 812: | 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 |
||
Line 820: | Line 820: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
;;; |
;;; |
||
;;; leo - calculates the first n number from a leo sequence. |
;;; leo - calculates the first n number from a leo sequence. |
||
Line 836: | Line 836: | ||
(cond ((= n 1) (list a)) |
(cond ((= n 1) (list a)) |
||
(T (iterate (- n 2) (list b a)))))) |
(T (iterate (- n 2) (list b a)))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 848: | Line 848: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ruby">def leonardo(l_zero, l_one, add, amount) |
||
terms = [l_zero, l_one] |
terms = [l_zero, l_one] |
||
while terms.size < amount |
while terms.size < amount |
||
Line 860: | Line 860: | ||
puts "First 25 Leonardo numbers: \n#{ leonardo(1,1,1,25) }" |
puts "First 25 Leonardo numbers: \n#{ leonardo(1,1,1,25) }" |
||
puts "Leonardo numbers with fibonacci parameters:\n#{ leonardo(0,1,0,25) }" |
puts "Leonardo numbers with fibonacci parameters:\n#{ leonardo(0,1,0,25) }" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 871: | Line 871: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
<syntaxhighlight lang="d"> |
|||
<lang D> |
|||
import std.stdio; |
import std.stdio; |
||
Line 892: | Line 892: | ||
writeln(); |
writeln(); |
||
} |
} |
||
</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 |
||
Line 900: | Line 900: | ||
=={{header|F#|F sharp}}== |
=={{header|F#|F sharp}}== |
||
{{trans|Haskell}} |
{{trans|Haskell}} |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let leo l0 l1 d = |
let leo l0 l1 d = |
||
Line 917: | Line 917: | ||
printfn "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number):\n%A" fibNums |
printfn "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number):\n%A" fibNums |
||
0 // return an integer exit code</ |
0 // return an integer exit code</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First 25 of the (1, 1, 1) Leonardo numbers: |
<pre>First 25 of the (1, 1, 1) Leonardo numbers: |
||
Line 930: | Line 930: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang>USING: fry io kernel math prettyprint sequences ; |
<syntaxhighlight lang="text">USING: fry io kernel math prettyprint sequences ; |
||
IN: rosetta-code.leonardo-numbers |
IN: rosetta-code.leonardo-numbers |
||
Line 942: | Line 942: | ||
"First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0:" print |
"First 25 Leonardo numbers with L(0)=0, L(1)=1, add=0:" print |
||
V{ 0 1 } 0 first25-leonardo print-leo</ |
V{ 0 1 } 0 first25-leonardo print-leo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 952: | Line 952: | ||
=={{header|Fermat}}== |
=={{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} |
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} |
leo[1,1]:=l0; leo[1,2]:=l1; {fermat arrays are 1-indexed} |
||
Line 964: | Line 964: | ||
Leonardo(25, 0, 1, 0); |
Leonardo(25, 0, 1, 0); |
||
[leo];</ |
[leo];</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 ]] |
||
Line 976: | Line 976: | ||
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. |
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 1,010: | Line 1,010: | ||
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 1,020: | Line 1,020: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Sub leonardo(L0 As Integer, L1 As Integer, suma As Integer, texto As String) |
Sub leonardo(L0 As Integer, L1 As Integer, suma As Integer, texto As String) |
||
Dim As Integer i, tmp |
Dim As Integer i, tmp |
||
Line 1,043: | Line 1,043: | ||
leonardo(0,1,0,"Fibonacci") |
leonardo(0,1,0,"Fibonacci") |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,054: | Line 1,054: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,073: | Line 1,073: | ||
fmt.Println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") |
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)) |
fmt.Println(leonardo(25, 0, 1, 0)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,085: | Line 1,085: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (intercalate, unfoldr) |
||
import Data.List.Split (chunksOf) |
import Data.List.Split (chunksOf) |
||
Line 1,109: | Line 1,109: | ||
] |
] |
||
where |
where |
||
f = unlines . fmap (('\t' :) . intercalate ",") . chunksOf 16 . fmap show</ |
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: |
||
Line 1,122: | Line 1,122: | ||
Alternately, defining the list self-referentially instead of using unfoldr: |
Alternately, defining the list self-referentially instead of using unfoldr: |
||
< |
<syntaxhighlight lang="haskell">leo :: Integer -> Integer -> Integer -> [Integer] |
||
leo l0 l1 d = s where |
leo l0 l1 d = s where |
||
s = l0 : l1 : zipWith (\x y -> x + y + d) s (tail s)</ |
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 1,141: | Line 1,141: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,166: | Line 1,166: | ||
System.out.println(leonardo(25, 0, 1, 0)); |
System.out.println(leonardo(25, 0, 1, 0)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
<pre>The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: |
||
Line 1,176: | Line 1,176: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">const leoNum = (c, l0 = 1, l1 = 1, add = 1) => |
||
new Array(c).fill(add).reduce( |
new Array(c).fill(add).reduce( |
||
(p, c, i) => i > 1 ? ( |
(p, c, i) => i > 1 ? ( |
||
Line 1,184: | Line 1,184: | ||
console.log(leoNum(25)); |
console.log(leoNum(25)); |
||
console.log(leoNum(25, 0, 1, 0));</ |
console.log(leoNum(25, 0, 1, 0));</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 1,193: | Line 1,193: | ||
Or, taking N terms from a non-finite Javascript generator: |
Or, taking N terms from a non-finite Javascript generator: |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,284: | Line 1,284: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>First 25 Leonardo numbers: |
<pre>First 25 Leonardo numbers: |
||
Line 1,296: | Line 1,296: | ||
=={{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 1,302: | Line 1,302: | ||
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 1,311: | Line 1,311: | ||
| .[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 1,317: | Line 1,317: | ||
'''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 1,328: | Line 1,328: | ||
{{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 1,345: | Line 1,345: | ||
# 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 1,354: | Line 1,354: | ||
=={{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 1,369: | Line 1,369: | ||
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 1,381: | Line 1,381: | ||
=={{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 1,400: | Line 1,400: | ||
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 1,410: | Line 1,410: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">L := proc(n, L_0, L_1, add) |
||
if n = 0 then |
if n = 0 then |
||
return L_0; |
return L_0; |
||
Line 1,422: | Line 1,422: | ||
Leonardo := n -> (L(1, 1, 1),[seq(0..n - 1)]) |
Leonardo := n -> (L(1, 1, 1),[seq(0..n - 1)]) |
||
Fibonacci := n -> (L(0, 1, 0), [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] |
<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|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">L[0,L0_:1,___]:=L0 |
||
L[1,L0_:1,L1_:1,___]:=L1 |
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[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/@(Range[25]-1) |
||
L[#,0,1,0]&/@(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} |
<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} |
||
Line 1,439: | Line 1,439: | ||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang="min">(over over + rolldown pop pick +) :next |
||
(('print dip " " print! next) 25 times newline) :leo |
(('print dip " " print! next) 25 times newline) :leo |
||
Line 1,445: | Line 1,445: | ||
1 1 1 leo |
1 1 1 leo |
||
"First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1:" puts! |
"First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1:" puts! |
||
0 0 1 leo</ |
0 0 1 leo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,455: | Line 1,455: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE Leonardo; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar; |
FROM Terminal IMPORT WriteString,WriteLn,ReadChar; |
||
Line 1,488: | Line 1,488: | ||
ReadChar |
ReadChar |
||
END Leonardo.</ |
END Leonardo.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
proc leonardoNumbers(count: int, L0: int = 1, |
proc leonardoNumbers(count: int, L0: int = 1, |
||
Line 1,509: | Line 1,509: | ||
leonardoNumbers(25) |
leonardoNumbers(25) |
||
echo "Fibonacci Numbers: " |
echo "Fibonacci Numbers: " |
||
leonardoNumbers(25, 0, 1, 0)</ |
leonardoNumbers(25, 0, 1, 0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,529: | Line 1,529: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">no warnings 'experimental::signatures'; |
||
use feature 'signatures'; |
use feature 'signatures'; |
||
Line 1,540: | Line 1,540: | ||
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 1,547: | Line 1,547: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<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: #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> |
||
Line 1,559: | Line 1,559: | ||
<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;">"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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,567: | Line 1,567: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">go => |
||
println([leonardo(I) : I in 0..24]), |
println([leonardo(I) : I in 0..24]), |
||
println([leonardo(0,1,0,I) : I in 0..24]). |
println([leonardo(0,1,0,I) : I in 0..24]). |
||
Line 1,575: | Line 1,575: | ||
leonardo(I1,_I2,_Add,0) = I1. |
leonardo(I1,_I2,_Add,0) = I1. |
||
leonardo(_I1,I2,_Add,1) = I2. |
leonardo(_I1,I2,_Add,1) = I2. |
||
leonardo(I1,I2,Add,N) = leonardo(I1,I2,Add,N-1) + leonardo(I1,I2,Add,N-2) + Add.</ |
leonardo(I1,I2,Add,N) = leonardo(I1,I2,Add,N-1) + leonardo(I1,I2,Add,N-2) + Add.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,583: | Line 1,583: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de leo (A B C) |
||
(default A 1 B 1 C 1) |
(default A 1 B 1 C 1) |
||
(make |
(make |
||
Line 1,592: | Line 1,592: | ||
(println 'Leonardo (leo)) |
(println 'Leonardo (leo)) |
||
(println 'Fibonacci (leo 0 1 0))</ |
(println 'Fibonacci (leo 0 1 0))</syntaxhighlight> |
||
{{out}} |
{{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) |
<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) |
||
Line 1,598: | Line 1,598: | ||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Write "First 25 Leonardo numbers:" on the console. |
Write "First 25 Leonardo numbers:" on the console. |
||
Line 1,619: | Line 1,619: | ||
Put the first number plus the second number plus the add number into 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. |
Write the second number then " " on the console without advancing. |
||
Repeat.</ |
Repeat.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,629: | Line 1,629: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic"> |
||
EnableExplicit |
EnableExplicit |
||
Line 1,659: | Line 1,659: | ||
r$ = Input() |
r$ = Input() |
||
EndIf |
EndIf |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,672: | Line 1,672: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Finite iteration=== |
===Finite iteration=== |
||
< |
<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 1,691: | Line 1,691: | ||
out += str(term) + " " |
out += str(term) + " " |
||
print out |
print out |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,703: | Line 1,703: | ||
Or, for a non-finite stream of Leonardos, we can use a Python generator: |
Or, for a non-finite stream of Leonardos, we can use a Python generator: |
||
{{Works with|Python|3}} |
{{Works with|Python|3}} |
||
< |
<syntaxhighlight lang="python">'''Leonardo numbers''' |
||
from functools import (reduce) |
from functools import (reduce) |
||
Line 1,778: | Line 1,778: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>First 25 Leonardo numbers: |
<pre>First 25 Leonardo numbers: |
||
Line 1,790: | Line 1,790: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ 1 1 1 ] is leo ( --> n n n ) |
||
[ 0 1 0 ] is fibo ( --> n n n ) |
[ 0 1 0 ] is fibo ( --> n n n ) |
||
Line 1,809: | Line 1,809: | ||
cr cr |
cr cr |
||
say "Lucas numbers:" cr |
say "Lucas numbers:" cr |
||
25 times [ i^ lucaso nardo echo sp ]</ |
25 times [ i^ lucaso nardo echo sp ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,825: | Line 1,825: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="rsplus"> |
||
leonardo_numbers <- function(add = 1, l0 = 1, l1 = 1, how_many = 25) { |
leonardo_numbers <- function(add = 1, l0 = 1, l1 = 1, how_many = 25) { |
||
result <- c(l0, l1) |
result <- c(l0, l1) |
||
Line 1,837: | Line 1,837: | ||
cat("First 25 Leonardo numbers from 0, 1 with add number = 0\n") |
cat("First 25 Leonardo numbers from 0, 1 with add number = 0\n") |
||
cat(leonardo_numbers(0, 0, 1), "\n") |
cat(leonardo_numbers(0, 0, 1), "\n") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,847: | Line 1,847: | ||
=={{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 1,865: | Line 1,865: | ||
(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}} |
||
Line 1,874: | Line 1,874: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub 𝑳 ( $𝑳0 = 1, $𝑳1 = 1, $𝑳add = 1 ) { $𝑳0, $𝑳1, { $^n2 + $^n1 + $𝑳add } ... * } |
||
# Part 1 |
# Part 1 |
||
Line 1,882: | Line 1,882: | ||
# Part 2 |
# Part 2 |
||
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:"; |
||
put 𝑳( 0, 1, 0 )[^25];</ |
put 𝑳( 0, 1, 0 )[^25];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>The first 25 Leonardo numbers: |
<pre>The first 25 Leonardo numbers: |
||
Line 1,891: | Line 1,891: | ||
=={{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 1,915: | Line 1,915: | ||
$=$ 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 1,935: | Line 1,935: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Leanardo numbers |
# Project : Leanardo numbers |
||
Line 1,959: | Line 1,959: | ||
next |
next |
||
see nl |
see nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,970: | Line 1,970: | ||
=={{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 1,980: | Line 1,980: | ||
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] |
||
Line 1,987: | Line 1,987: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">sqliteconnect #mem, ":memory:" |
||
#mem execute("CREATE TABLE lno (name,L0,L1,ad)") |
#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("INSERT INTO lno VALUES('Leonardo',1,1,1),('Fibonacci',0,1,0);") |
||
Line 2,005: | Line 2,005: | ||
next i |
next i |
||
next j |
next j |
||
end</ |
end</syntaxhighlight> |
||
<pre>Leonardo add=1 |
<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 |
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 2,013: | Line 2,013: | ||
=={{header|Rust}}== |
=={{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 || { |
std::iter::from_fn(move || { |
||
let n = n0; |
let n = n0; |
||
Line 2,033: | Line 2,033: | ||
} |
} |
||
println!(); |
println!(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,044: | Line 2,044: | ||
=={{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 2,057: | Line 2,057: | ||
(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 2,067: | Line 2,067: | ||
=={{header|Seed7}}== |
=={{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 |
const proc: leonardo (in var integer: l0, in var integer: l1, in integer: add, in integer: count) is func |
||
Line 2,088: | Line 2,088: | ||
write("Fibonacci Numbers:"); |
write("Fibonacci Numbers:"); |
||
leonardo(0, 1, 0, 25); |
leonardo(0, 1, 0, 25); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,097: | Line 2,097: | ||
=={{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 2,106: | Line 2,106: | ||
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 2,117: | Line 2,117: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">struct Leonardo: Sequence, IteratorProtocol { |
||
private let add : Int |
private let add : Int |
||
private var n0: Int |
private var n0: Int |
||
Line 2,140: | Line 2,140: | ||
print("First 25 Fibonacci numbers:") |
print("First 25 Fibonacci numbers:") |
||
print(Leonardo(n0: 0, add: 0).prefix(25).map{String($0)}.joined(separator: " "))</ |
print(Leonardo(n0: 0, add: 0).prefix(25).map{String($0)}.joined(separator: " "))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,152: | Line 2,152: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 2,186: | Line 2,186: | ||
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 2,197: | Line 2,197: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Iterator Function Leonardo(Optional L0 = 1, Optional L1 = 1, Optional add = 1) As IEnumerable(Of Integer) |
Iterator Function Leonardo(Optional L0 = 1, Optional L1 = 1, Optional add = 1) As IEnumerable(Of Integer) |
||
Line 2,213: | Line 2,213: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</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 |
||
Line 2,220: | Line 2,220: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">fn leonardo(n int, l0 int, l1 int, add int) []int { |
||
mut leo := []int{len: n} |
mut leo := []int{len: n} |
||
leo[0] = l0 |
leo[0] = l0 |
||
Line 2,235: | Line 2,235: | ||
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)) |
println(leonardo(25, 0, 1, 0)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,248: | Line 2,248: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var leonardo = Fn.new { |first, add, limit| |
||
var leo = List.filled(limit, 0) |
var leo = List.filled(limit, 0) |
||
leo[0] = first[0] |
leo[0] = first[0] |
||
Line 2,261: | Line 2,261: | ||
System.print("\n\nThe first 25 Leonardo numbers with L(0) = 0, L(1) = 1 and Add = 0 are:") |
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) ") |
for (l in leonardo.call([0, 1], 0, 25)) System.write("%(l) ") |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,274: | Line 2,274: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">limit = 25 |
||
sub leonardo(L0, L1, suma, texto$) |
sub leonardo(L0, L1, suma, texto$) |
||
Line 2,294: | Line 2,294: | ||
leonardo(1,1,1,"Leonardo") |
leonardo(1,1,1,"Leonardo") |
||
leonardo(0,1,0,"Fibonacci") |
leonardo(0,1,0,"Fibonacci") |
||
end</ |
end</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">int N, L, L0, L1, Add; |
||
[Text(0, "Enter L(0), L(1), Add: "); |
[Text(0, "Enter L(0), L(1), Add: "); |
||
L0:= IntIn(0); |
L0:= IntIn(0); |
||
Line 2,311: | Line 2,311: | ||
L1:= L; |
L1:= L; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,322: | Line 2,322: | ||
=={{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 2,333: | Line 2,333: | ||
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> |