Leonardo numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(14 intermediate revisions by 8 users not shown)
Line 896:
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>
 
Line 1,052 ⟶ 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}}==
Line 1,436 ⟶ 1,621:
<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}}==
Line 1,527 ⟶ 1,734:
</pre>
 
=={{header|PerlOCaml}}==
<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,544 ⟶ 1,770:
<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>
 
Line 1,967 ⟶ 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}}==
Line 2,247 ⟶ 2,557:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var leonardo = Fn.new { |first, add, limit|
var leo = List.filled(limit, 0)
leo[0] = first[0]
Line 2,270 ⟶ 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}}==
9,476

edits