Leonardo numbers: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (changed THE to THIS (task).) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(45 intermediate revisions by 26 users not shown) | |||
Line 2:
[[Category:Classic CS problems and programs]]
<!-- The following <math> tag doesn't render properly as it does correctly on Wikipedia.
It generates some bold red error messages.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:<math>
L(n) =
\begin{cases}
Line 17:
</math>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(end of comment) -->
''Leonardo numbers'' are also known as the ''Leonardo series''.
The '''Leonardo numbers''' are a sequence of numbers defined by:
Line 34 ⟶ 38:
[[wp:Edsger W. Dijkstra|Edsger W. Dijkstra]] used
his [[wp:smoothsort|smoothsort]] [[wp:algorithm|algorithm]].
Line 59 ⟶ 63:
Show all output here on this page.
Line 72 ⟶ 76:
* [[oeis:A001595|OEIS Leonardo numbers]]
<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}}==
<
procedure Leonardo is
Line 103 ⟶ 180:
end loop;
New_Line;
end Leonardo;</
{{out}}
<pre>
Line 113 ⟶ 190:
=={{header|ALGOL 68}}==
<
# leonardo number parameters #
MODE LEONARDO = STRUCT( INT l0, l1, add number );
Line 154 ⟶ 231:
show( 25, leonardo numbers WITHLZERO 0 WITHADDNUMBER 0 );
print( ( newline ) )
END</
{{out}}
<pre>
Line 164 ⟶ 241:
=={{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
Line 182 ⟶ 262:
--
on run
set leonardo to leo(1, 1, 1)
Line 194 ⟶ 274:
--
-- twoLines :: [Int] -> String
Line 200 ⟶ 280:
script row
on |λ|(ns)
tab &
end |λ|
end script
Line 207 ⟶ 287:
--
-- chunksOf :: Int -> [a] -> [[a]]
Line 225 ⟶ 304:
foldl(go, {}, enumFromThenTo(1, n, lng))
end chunksOf
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
Line 235 ⟶ 315:
return xs
end enumFromThenTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 247 ⟶ 328:
end tell
end foldl
-- intercalate :: String -> [String] -> String
Line 256 ⟶ 338:
return s
end intercalate
-- Lift 2nd class handler function into 1st class script wrapper
Line 268 ⟶ 351:
end if
end mReturn
-- map :: (a -> b) -> [a] -> [b]
Line 280 ⟶ 364:
end tell
end map
-- max :: Ord a => a -> a -> a
Line 290 ⟶ 375:
end max
-- take :: Int -> [a] -> [a]
Line 326 ⟶ 407:
end if
end take
-- unlines :: [String] -> String
Line 334 ⟶ 416:
set my text item delimiters to dlm
str
end unlines</
{{Out}}
<pre>First 25 Leonardo numbers:
First 25 Fibonacci numbers:
----
===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 {
Line 370 ⟶ 536:
printf("\n")
}
</syntaxhighlight>
{{out}}
<pre>
Line 380 ⟶ 546:
=={{header|Bash}}==
<syntaxhighlight lang="bash">
#!/bin/bash
Line 394 ⟶ 560:
echo "${leonardo_numbers[*]}"
}
</syntaxhighlight>
{{out}}
<pre>
Line 406 ⟶ 572:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">
subroutine leonardo(L0, L1, suma, texto)
print "Numeros de " + texto + " (" + L0 + "," + L1 + "," + suma + "):"
Line 430 ⟶ 596:
call leonardo(0,1,0,"Fibonacci")
end
</syntaxhighlight>
{{out}}
<pre>
Line 441 ⟶ 607:
==={{header|IS-BASIC}}===
<
110 INPUT PROMPT "Enter values of L0, L1, and ADD, separated by comas: ":L0,L1,ADD
120 PRINT L0;L1;
Line 448 ⟶ 614:
160 PRINT L1;
170 NEXT
180 PRINT</
==={{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.
<
20 INPUT L1
30 INPUT ADD
Line 461 ⟶ 627:
80 LET L0=TEMP
90 PRINT " ";L1;
100 NEXT I</
{{in}}
<pre>1
Line 477 ⟶ 643:
=={{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.
<
:
PRINT "Enter values of L0, L1, and ADD, separated by commas:"
Line 489 ⟶ 655:
NEXT
PRINT
END</
{{out}}
<pre>Enter values of L0, L1, and ADD, separated by commas:
Line 547 ⟶ 713:
=={{header|Burlesque}}==
<
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</
=={{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.
<syntaxhighlight lang="c">
#include<stdio.h>
Line 590 ⟶ 756:
return 0;
}
</syntaxhighlight>
Output :
Normal Leonardo Series :
Line 607 ⟶ 773:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<
using System.Linq;
Line 623 ⟶ 789:
}
}
}</
{{out}}
<pre>
Line 631 ⟶ 797:
=={{header|C++}}==
<
#include <iostream>
Line 646 ⟶ 812:
return 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
Line 654 ⟶ 820:
=={{header|Common Lisp}}==
<
;;;
;;; leo - calculates the first n number from a leo sequence.
Line 670 ⟶ 836:
(cond ((= n 1) (list a))
(T (iterate (- n 2) (list b a))))))
</syntaxhighlight>
{{out}}
Line 682 ⟶ 848:
=={{header|Crystal}}==
{{trans|Python}}
<
terms = [l_zero, l_one]
while terms.size < amount
Line 694 ⟶ 860:
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>
Line 705 ⟶ 871:
=={{header|D}}==
{{trans|C++}}
<syntaxhighlight lang="d">
import std.stdio;
Line 726 ⟶ 892:
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}}
<
let leo l0 l1 d =
Line 751 ⟶ 1,025:
printfn "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci number):\n%A" fibNums
0 // return an integer exit code</
{{out}}
<pre>First 25 of the (1, 1, 1) Leonardo numbers:
Line 764 ⟶ 1,038:
=={{header|Factor}}==
<syntaxhighlight lang="text">USING: fry io kernel math prettyprint sequences ;
IN: rosetta-code.leonardo-numbers
Line 775 ⟶ 1,049:
V{ 1 1 } 1 first25-leonardo print-leo
"First 25 Leonardo numbers with L(0)=0, L(1)=1, add=
V{ 0 1 } 0 first25-leonardo print-leo</
{{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 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>
Line 790 ⟶ 1,084:
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. <
INTEGER LAST !Limit to show.
INTEGER L0,L1 !Starting values.
Line 824 ⟶ 1,118:
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:
<pre>
Line 834 ⟶ 1,128:
=={{header|FreeBASIC}}==
<
Sub leonardo(L0 As Integer, L1 As Integer, suma As Integer, texto As String)
Dim As Integer i, tmp
Line 857 ⟶ 1,151:
leonardo(0,1,0,"Fibonacci")
End
</syntaxhighlight>
{{out}}
<pre>
Line 866 ⟶ 1,160:
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}}==
<
import "fmt"
Line 887 ⟶ 1,258:
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))
}</
{{out}}
Line 899 ⟶ 1,270:
=={{header|Haskell}}==
<
import Data.List.Split (
-- L0 -> L1 -> Add number -> Series (infinite)
leo :: Integer -> Integer -> Integer -> [Integer]
Line 913 ⟶ 1,284:
fibonacci = leo 0 1 0
main :: IO ()
main =
(putStrLn . unlines)
[ "First 25 default (1, 1, 1) Leonardo numbers:\n"
,
, "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers):\n"
,
]
where
f = unlines . fmap (('\t' :) . intercalate ",") . chunksOf 16 . fmap show</syntaxhighlight>
{{Out}}
<pre>First 25 default (1, 1, 1) Leonardo numbers:
First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers):
Alternately, defining the list self-referentially instead of using unfoldr:
<
leo l0 l1 d = s where
s = l0 : l1 : zipWith (\x y -> x + y + d) s (tail s)</
=={{header|J}}==
<syntaxhighlight lang="j">
leo =: (] , {.@[ + _2&{@] + {:@])^:(_2&+@{:@[)
</syntaxhighlight>
{{Out}}
<pre>
Line 954 ⟶ 1,326:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.List;
Line 979 ⟶ 1,351:
System.out.println(leonardo(25, 0, 1, 0));
}
}</
{{out}}
<pre>The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:
Line 989 ⟶ 1,361:
=={{header|JavaScript}}==
===ES6===
<
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>
Line 1,003 ⟶ 1,378:
Or, taking N terms from a non-finite Javascript generator:
{{Trans|Python}}
<
'use strict';
Line 1,011 ⟶ 1,386:
while (true) {
yield x;
[x, y] = [y,
}
}
// ----------------------- TEST ------------------------
// main :: IO ()
const main = () => {
Line 1,021 ⟶ 1,397:
fibonacci = leo(0, 1, 0);
'First 25 Fibonacci
]);
};
// -------------------- FORMATTING ---------------------
// indentWrapped :: [Int] -> String
const indentWrapped = xs =>
map(x => '\t' + x.join(','))(
)
);
// ----------------- GENERIC FUNCTIONS -----------------
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf =
xs => enumFromThenTo(0
xs.
(a, i) => a.concat([xs.slice(i, (n + i))]),
[]
);
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo =
}, (_, i) => x1 + (d * i));
};
// map :: (a -> b) -> [a] -> [b]
const map =
// The list obtained by applying f
// to each element of xs.
// (The image of xs under f).
xs => [...xs].map(f);
//
const
x.toString();
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take =
// 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({
Line 1,084 ⟶ 1,469:
// MAIN ---
return main();
})();</
{{Out}}
<pre>First 25 Leonardo numbers:
First 25 Fibonacci numbers:
=={{header|jq}}==
===Naive Implementation===
<
def leo:
if . == 0 then zero
Line 1,102 ⟶ 1,487:
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 leo(n):
if .[n] then .
Line 1,111 ⟶ 1,496:
| .[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.)
Line 1,117 ⟶ 1,502:
'''Examples'''
<
{{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>
<
{{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>
Line 1,128 ⟶ 1,513:
{{works with|Julia|0.6}}
<
l = max(maximum(n) .+ 1, length(firsts))
r = Vector{Int}(l)
Line 1,145 ⟶ 1,530:
# Task 4
println("First 25 Leonardo numbers starting with [0, 1]: ", join(L(0:24, 0, [0, 1]), ", "))</
{{out}}
Line 1,154 ⟶ 1,539:
=={{header|Kotlin}}==
<
fun leonardo(n: Int, l0: Int = 1, l1: Int = 1, add: Int = 1): IntArray {
Line 1,169 ⟶ 1,554:
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(" "))
}</
{{out}}
Line 1,181 ⟶ 1,566:
=={{header|Lua}}==
<
local L0, L1, add = L0 or 1, L1 or 1, add or 1
local lNums, nextNum = {L0, L1}
Line 1,200 ⟶ 1,585:
show("Leonardo numbers", leoNums(25))
show("Fibonacci numbers", leoNums(25, 0, 1, 0))</
{{out}}
<pre>Leonardo numbers:
Line 1,210 ⟶ 1,595:
=={{header|Maple}}==
<
if n = 0 then
return L_0;
Line 1,222 ⟶ 1,607:
Leonardo := n -> (L(1, 1, 1),[seq(0..n - 1)])
Fibonacci := n -> (L(0, 1, 0), [seq(0..n - 1)])</
<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)</
<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}}
<
(('print dip " " print! next) 25 times newline) :leo
Line 1,246 ⟶ 1,652:
1 1 1 leo
"First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1:" puts!
0 0 1 leo</
{{out}}
<pre>
Line 1,256 ⟶ 1,662:
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 1,289 ⟶ 1,695:
ReadChar
END Leonardo.</
=={{header|Nim}}==
<
proc leonardoNumbers(count: int, L0: int = 1,
Line 1,310 ⟶ 1,716:
leonardoNumbers(25)
echo "Fibonacci Numbers: "
leonardoNumbers(25, 0, 1, 0)</
{{out}}
<pre>
Line 1,328 ⟶ 1,734:
</pre>
=={{header|
<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';
Line 1,341 ⟶ 1,766:
print "Leonardo[1,1,1]: @L\n";
my @F = map { leonardo($_,0,1,0) } 0..24;
print "Leonardo[0,1,0]: @F\n";</
{{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
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
</pre>
=={{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:
[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}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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: #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>
Line 1,363 ⟶ 1,827:
{"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>
{{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|PicoLisp}}==
<
(default A 1 B 1 C 1)
(make
Line 1,374 ⟶ 1,854:
(println 'Leonardo (leo))
(println 'Fibonacci (leo 0 1 0))</
{{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
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}}==
<
EnableExplicit
Line 1,410 ⟶ 1,921:
r$ = Input()
EndIf
</syntaxhighlight>
{{out}}
<pre>
Line 1,423 ⟶ 1,934:
=={{header|Python}}==
===Finite iteration===
<
terms = [L_Zero,L_One]
while len(terms) < Amount:
Line 1,442 ⟶ 1,953:
out += str(term) + " "
print out
</syntaxhighlight>
{{out}}
<pre>
Line 1,454 ⟶ 1,965:
Or, for a non-finite stream of Leonardos, we can use a Python generator:
{{Works with|Python|3}}
<
from functools import (reduce)
Line 1,529 ⟶ 2,040:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 25 Leonardo numbers:
Line 1,538 ⟶ 2,049:
[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}}==
<
leonardo_numbers <- function(add = 1, l0 = 1, l1 = 1, how_many = 25) {
result <- c(l0, l1)
Line 1,552 ⟶ 2,099:
cat("First 25 Leonardo numbers from 0, 1 with add number = 0\n")
cat(leonardo_numbers(0, 0, 1), "\n")
</syntaxhighlight>
{{out}}
<pre>
Line 1,562 ⟶ 2,109:
=={{header|Racket}}==
<
(define (Leonardo n #:L0 (L0 1) #:L1 (L1 1) #:1+ (1+ 1))
(cond [(= n 0) L0]
Line 1,580 ⟶ 2,127:
(check-equal? (Leonardo 1) 1)
(check-equal? (Leonardo 2) 3)
(check-equal? (Leonardo 3) 5))</
{{out}}
Line 1,589 ⟶ 2,136:
(formerly Perl 6)
<syntaxhighlight lang="raku"
# Part 1
Line 1,597 ⟶ 2,144:
# Part 2
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:";
put 𝑳( 0, 1, 0 )[^25];</
{{out}}
<pre>The first 25 Leonardo numbers:
Line 1,606 ⟶ 2,153:
=={{header|REXX}}==
<
numeric digits 500 /*just in case the user gets ka-razy. */
@.=1 /*define the default for the @. array.*/
Line 1,630 ⟶ 2,177:
$=$ 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. */</
{{out|output|text= when using the default input:}}
<pre>
Line 1,650 ⟶ 2,197:
=={{header|Ring}}==
<
# Project : Leanardo numbers
Line 1,674 ⟶ 2,221:
next
see nl
</syntaxhighlight>
Output:
<pre>
Line 1,682 ⟶ 2,229:
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|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}}==
Enumerators are nice for this.
<
return to_enum(__method__,l0,l1,add) unless block_given?
loop do
Line 1,695 ⟶ 2,290:
p leonardo.take(25)
p leonardo(0,1,0).take(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]
Line 1,702 ⟶ 2,297:
=={{header|Run BASIC}}==
<
#mem execute("CREATE TABLE lno (name,L0,L1,ad)")
#mem execute("INSERT INTO lno VALUES('Leonardo',1,1,1),('Fibonacci',0,1,0);")
Line 1,720 ⟶ 2,315:
next i
next j
end</
<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>
=={{header|Scala}}==
<
case 0 => n1
case 1 => n2
Line 1,741 ⟶ 2,367:
(0 until 25) foreach { n => print( leo(n, n1=0, n2=1, addnum=0) + " " ) }
}
</syntaxhighlight>
{{out}}
<pre>The first 25 Leonardo Numbers:
Line 1,751 ⟶ 2,377:
=={{header|Seed7}}==
<
const proc: leonardo (in var integer: l0, in var integer: l1, in integer: add, in integer: count) is func
Line 1,772 ⟶ 2,398:
write("Fibonacci Numbers:");
leonardo(0, 1, 0, 25);
end func;</
{{out}}
Line 1,781 ⟶ 2,407:
=={{header|Sidef}}==
<
{ (𝑳0, 𝑳1) = (𝑳1, 𝑳0 + 𝑳1 + 𝑳add) } * n
return 𝑳0
Line 1,790 ⟶ 2,416:
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:"
say 25.of { 𝑳(_, 0, 1, 0) }</
{{out}}
<pre>
Line 1,798 ⟶ 2,424:
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|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>
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Option Explicit
Line 1,836 ⟶ 2,496:
Leo_Numbers = Ltemp
End Function
</syntaxhighlight>
{{out}}
<pre>First 25 Leonardo numbers :
Line 1,847 ⟶ 2,507:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Iterator Function Leonardo(Optional L0 = 1, Optional L1 = 1, Optional add = 1) As IEnumerable(Of Integer)
Line 1,863 ⟶ 2,523:
End Sub
End Module</
{{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}}==
<
var leo = List.filled(limit, 0)
leo[0] = first[0]
Line 1,882 ⟶ 2,570:
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()</
{{out}}
Line 1,892 ⟶ 2,580:
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}}==
<
[Text(0, "Enter L(0), L(1), Add: ");
L0:= IntIn(0);
Line 1,907 ⟶ 2,619:
L1:= L;
];
]</
{{out}}
Line 1,918 ⟶ 2,630:
=={{header|zkl}}==
<
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
}</
<
foreach n in (25){ print(leonardoNumber(n)," ") }
println("\n");
Line 1,929 ⟶ 2,641:
println("The first 25 Fibonacci Numbers:");
foreach n in (25){ print(leonardoNumber(n, 0,1,0)," ") }
println();</
{{out}}
<pre>
|