Leonardo numbers
You are encouraged to solve this task according to the task description, using any language you may know.
The Leonardo numbers are a sequence of numbers defined by:
L(0) = 1 L(1) = 1 L(n) = L(n-1) + L(n-2) + 1 also L(n) = 2 * Fib(n+1) - 1
- where the + 1 will herein be known as the add number.
- where the FIB is the Fibonacci numbers.
The task will be using the 3rd equation (above) to calculate the Leonardo numbers.
Edsger W. Dijkstra used them as an integral part of
his smoothsort algorithm.
The first few Leonardo numbers are:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 ···
- Task
-
- show the 1st 25 Leonardo numbers, starting at L(0).
- allow the first two Leonardo numbers to be specified [for L(0) and L(1)].
- allow the add number to be specified (1 is the default).
- show the 1st 25 Leonardo numbers, specifying 0 and 1 for L(0) and L(1), and 0 for the add number.
(The last task requirement will produce the Fibonacci numbers.)
Show all output here.
- Related tasks
- See also
ALGOL 68
<lang algol68>BEGIN
# leonardo number parameters # MODE LEONARDO = STRUCT( INT l0, l1, add number ); # default leonardo number parameters # LEONARDO leonardo numbers = LEONARDO( 1, 1, 1 ); # operators to allow us to specify non-default parameters # PRIO WITHLZERO = 9, WITHLONE = 9, WITHADDNUMBER = 9; OP WITHLZERO = ( LEONARDO parameters, INT l0 )LEONARDO: LEONARDO( l0, l1 OF parameters, add number OF parameters ); OP WITHLONE = ( LEONARDO parameters, INT l1 )LEONARDO: LEONARDO( l0 OF parameters, l1, add number OF parameters ); OP WITHADDNUMBER = ( LEONARDO parameters, INT add number )LEONARDO: LEONARDO( l0 OF parameters, l1 OF parameters, add number ); # show the first n Leonardo numbers with the specified parameters # PROC show = ( INT n, LEONARDO parameters )VOID: IF n > 0 THEN INT l0 = l0 OF parameters; INT l1 = l1 OF parameters; INT add number = add number OF parameters; print( ( whole( l0, 0 ), " " ) ); IF n > 1 THEN print( ( whole( l1, 0 ), " " ) ); INT lp := l0; INT ln := l1; FROM 2 TO n - 1 DO INT next = ln + lp + add number; lp := ln; ln := next; print( ( whole( ln, 0 ), " " ) ) OD FI FI # show # ;
# first series # print( ( "First 25 Leonardo numbers", newline ) ); show( 25, leonardo numbers ); print( ( newline ) ); # second series # print( ( "First 25 Leonardo numbers from 0, 1 with add number = 0", newline ) ); show( 25, leonardo numbers WITHLZERO 0 WITHADDNUMBER 0 ); print( ( newline ) )
END</lang>
- Output:
First 25 Leonardo numbers 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 First 25 Leonardo numbers from 0, 1 with add number = 0 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
BASIC
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. <lang basic> 10 INPUT L0
20 INPUT L1 30 INPUT ADD 40 PRINT L0;" ";L1; 50 FOR I=3 TO 25 60 LET TEMP=L1 70 LET L1=L0+L1+ADD 80 LET L0=TEMP 90 PRINT " ";L1;
100 NEXT I</lang>
- Input:
1 1 1
- Output:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
- Input:
0 1 0
- Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
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. <lang C> /*Abhishek Ghosh, 23rd September 2017*/
- include<stdio.h>
void leonardo(int a,int b,int step,int num){
int i,temp;
printf("First 25 Leonardo numbers : \n");
for(i=1;i<=num;i++){ if(i==1) printf(" %d",a); else if(i==2) printf(" %d",b); else{ printf(" %d",a+b+step); temp = a; a = b; b = temp+b+step; } } }
int main() { int a,b,step;
printf("Enter first two Leonardo numbers and increment step : ");
scanf("%d%d%d",&a,&b,&step);
leonardo(a,b,step,25);
return 0; } </lang> Output : Normal Leonardo Series :
Enter first two Leonardo numbers and increment step : 1 1 1 First 25 Leonardo numbers : 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
Fibonacci Series :
Enter first two Leonardo numbers and increment step : 0 1 0 First 25 Leonardo numbers : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
C++
<lang cpp>
- include <iostream>
void leoN( int cnt, int l0 = 1, int l1 = 1, int add = 1 ) {
int t; for( int i = 0; i < cnt; i++ ) { std::cout << l0 << " "; t = l0 + l1 + add; l0 = l1; l1 = t; }
} int main( int argc, char* argv[] ) {
std::cout << "Leonardo Numbers: "; leoN( 25 ); std::cout << "\n\nFibonacci Numbers: "; leoN( 25, 0, 1, 0 ); return 0;
} </lang>
- Output:
Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Fortran
Happily, no monster values result for the trial run, so ordinary 32-bit integers suffice. The source style uses the F90 facilities only to name the subroutine being ended (i.e. END SUBROUTINE LEONARDO
rather than just END
) and the I0 format code that shows an integer without a fixed space allowance, convenient in produced well-formed messages. The "$" format code signifies that the end of output from its WRITE statement should not trigger the starting of a new line for the next WRITE statement, convenient when rolling a sequence of values to a line of output one-by-one as they are concocted. Otherwise, the values would have to be accumulated in a suitable array and then written in one go.
Many versions of Fortran have enabled parameters to be optionally supplied and F90 has standardised a protocol, also introducing a declaration syntax that can specify multiple attributes in one statement which in this case would be INTEGER, OPTIONAL:: AF
rather than two statements concerning AF. However, in a test run with CALL LEONARDO(25,1,1)
the Compaq F90/95 compiler rejected this attempt because there was another invocation with four parameters, not three, in the same program unit. By adding the rigmarole for declaring a MODULE containing the subroutine LEONARDO, its worries were 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. <lang Fortran> SUBROUTINE LEONARDO(LAST,L0,L1,AF) !Show the first LAST values of the sequence.
INTEGER LAST !Limit to show. INTEGER L0,L1 !Starting values. INTEGER AF !The "Add factor" to deviate from Fibonacci numbers. OPTIONAL AF !Indicate that this parameter may be omitted. INTEGER EMBOLISM !The bloat to employ. INTEGER N,LN,LNL1,LNL2 !Assistants to the calculation. IF (PRESENT(AF)) THEN !Perhaps the last parameter has not been given. EMBOLISM = AF !It has. Take its value. ELSE !But if not, EMBOLISM = 1 !This is the specified default. END IF !Perhaps there should be some report on this? WRITE (6,1) LAST,L0,L1,EMBOLISM !Announce. 1 FORMAT ("The first ",I0, !The I0 format code avoids excessive spacing. 1 " numbers in the Leonardo sequence defined by L(0) = ",I0, 2 " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0) IF (LAST .GE. 1) WRITE (6,2) L0 !In principle, LAST may be small. IF (LAST .GE. 2) WRITE (6,2) L1 !!So, suspicion rules. 2 FORMAT (I0,", ",$) !Obviously, the $ sez "don't finish the line". LNL1 = L0 !Syncopation for the sequence's initial values. LN = L1 !Since the parameters ought not be damaged. DO N = 3,LAST !Step away. LNL2 = LNL1 !Advance the two state variables one step. LNL1 = LN !Ready to make a step forward. LN = LNL1 + LNL2 + EMBOLISM !Thus. WRITE (6,2) LN !Reveal the value. Overflow is distant... END DO !On to the next step. WRITE (6,*) !Finish the line. END SUBROUTINE LEONARDO !Only speedy for the sequential production of values.
PROGRAM POKE
CALL LEONARDO(25,1,1,1) !The first 25 Leonardo numbers. CALL LEONARDO(25,0,1,0) !Deviates to give the Fibonacci sequence. END </lang>
Output:
The first 25 numbers in the Leonardo sequence defined by L(0) = 1 and L(1) = 1 with L(n) = L(n - 1) + L(n - 2) + 1 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049, The first 25 numbers in the Leonardo sequence defined by L(0) = 0 and L(1) = 1 with L(n) = L(n - 1) + L(n - 2) + 0 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368,
Haskell
<lang Haskell>import Data.List.Split (chunksOf) import Data.List (unfoldr)
-- LEONARDO NUMBERS ----------------------------------------------------------- -- L0 -> L1 -> Add number -> Series (infinite) leo :: Integer -> Integer -> Integer -> [Integer] leo l0 l1 d = unfoldr (\(x, y) -> Just (x, (y, x + y + d))) (l0, l1)
leonardo :: [Integer] leonardo = leo 1 1 1
fibonacci :: [Integer] fibonacci = leo 0 1 0
-- TEST ----------------------------------------------------------------------- main :: IO () main = do
let twoLines = unlines . fmap (('\t' :) . show) . chunksOf 16 (putStrLn . unlines) [ "First 25 default (1, 1, 1) Leonardo numbers:\n" , twoLines $ take 25 leonardo , "First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers):\n" , twoLines $ take 25 fibonacci ]</lang>
- Output:
First 25 default (1, 1, 1) Leonardo numbers: [1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973] [3193,5167,8361,13529,21891,35421,57313,92735,150049] First 25 of the (0, 1, 0) Leonardo numbers (= Fibonacci numbers): [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610] [987,1597,2584,4181,6765,10946,17711,28657,46368]
J
<lang J> leo =: (] , {.@[ + _2&{@] + {:@])^:(_2&+@{:@[) </lang>
- Output:
1 25 leo 1 1 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 0 25 leo 0 1 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
jq
Naive Implementation
<lang jq>def Leonardo(zero; one; incr):
def leo: if . == 0 then zero elif . == 1 then one else ((.-1) |leo) + ((.-2) | leo) + incr end; leo;</lang>
Implementation with Caching
An array is used for caching, with `.[n]` storing the value L(n). <lang jq>def Leonardo(zero; one; incr):
def leo(n): if .[n] then . else leo(n-1) # optimization of leo(n-2)|leo(n-1) | .[n] = .[n-1] + .[n-2] + incr end; . as $n | [zero,one] | leo($n) | .[$n];</lang>
(To compute the sequence of Leonardo numbers L(1) ... L(n) without redundant computation, the last element of the pipeline in the last line of the function above should be dropped.)
Examples
<lang jq>[range(0;25) | Leonardo(1;1;1)]</lang>
- Output:
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973,3193,5167,8361,13529,21891,35421,57313,92735,150049]
<lang jq>[range(0;25) | Leonardo(0;1;0)]</lang>
- Output:
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368]
Julia
<lang julia>function L(n, add::Int=1, firsts::Vector=[1, 1])
l = max(maximum(n) .+ 1, length(firsts)) r = Vector{Int}(l) r[1:length(firsts)] = firsts for i in 3:l r[i] = r[i - 1] + r[i - 2] + add end return r[n .+ 1]
end
- Task 1
println("First 25 Leonardo numbers: ", join(L(0:24), ", "))
- Task 2
@show L(0) L(1)
- Task 4
println("First 25 Leonardo numbers starting with [0, 1]: ", join(L(0:24, 0, [0, 1]), ", "))</lang>
- Output:
First 25 Leonardo numbers: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049 L(0) = 1 L(1) = 1 First 25 Leonardo numbers starting with 0, 1: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368
Kotlin
<lang scala>// version 1.1.2
fun leonardo(n: Int, l0: Int = 1, l1: Int = 1, add: Int = 1): IntArray {
val leo = IntArray(n) leo[0] = l0 leo[1] = l1 for (i in 2 until n) leo[i] = leo[i - 1] + leo[i - 2] + add return leo
}
fun main(args: Array<String>) {
println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:") println(leonardo(25).joinToString(" ")) println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:") println(leonardo(25, 0, 1, 0).joinToString(" "))
}</lang>
- Output:
The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Lua
<lang lua>function leoNums (n, L0, L1, add)
local L0, L1, add = L0 or 1, L1 or 1, add or 1 local lNums, nextNum = {L0, L1} while #lNums < n do nextNum = lNums[#lNums] + lNums[#lNums - 1] + add table.insert(lNums, nextNum) end return lNums
end
function show (msg, t)
print(msg .. ":") for i, x in ipairs(t) do io.write(x .. " ") end print("\n")
end
show("Leonardo numbers", leoNums(25)) show("Fibonacci numbers", leoNums(25, 0, 1, 0))</lang>
- Output:
Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Fibonacci numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Perl
<lang perl>no warnings 'experimental::signatures'; use feature 'signatures';
sub leonardo ($n, $l0 = 1, $l1 = 1, $add = 1) {
($l0, $l1) = ($l1, $l0+$l1+$add) for 1..$n; $l0;
}
my @L = map { leonardo($_) } 0..24; print "Leonardo[1,1,1]: @L\n"; my @F = map { leonardo($_,0,1,0) } 0..24; print "Leonardo[0,1,0]: @F\n";</lang>
- Output:
Leonardo[1,1,1]: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Leonardo[0,1,0]: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Perl 6
<lang perl6>sub 𝑳 ( $𝑳0 = 1, $𝑳1 = 1, $𝑳add = 1 ) { $𝑳0, $𝑳1, { $^n2 + $^n1 + $𝑳add } ... * }
- Part 1
say "The first 25 Leonardo numbers:"; put 𝑳()[^25];
- Part 2
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:"; put 𝑳( 0, 1, 0 )[^25];</lang>
- Output:
The first 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Python
<lang python>def Leonardo(L_Zero, L_One, Add, Amount):
terms = [L_Zero,L_One] while len(terms) < Amount: new = terms[-1] + terms[-2] new += Add terms.append(new) return terms
out = "" print "First 25 Leonardo numbers:" for term in Leonardo(1,1,1,25):
out += str(term) + " "
print out
out = "" print "Leonardo numbers with fibonacci parameters:" for term in Leonardo(0,1,0,25):
out += str(term) + " "
print out </lang>
- Output:
First 25 Leonardo numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 Leonardo numbers with fibonacci parameters: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Racket
<lang racket>#lang racket (define (Leonardo n #:L0 (L0 1) #:L1 (L1 1) #:1+ (1+ 1))
(cond [(= n 0) L0] [(= n 1) L1] [else (let inr ((n (- n 2)) (L_n-2 L0) (L_n-1 L1)) (let ((L_n (+ L_n-1 L_n-2 1+))) (if (zero? n) L_n (inr (sub1 n) L_n-1 L_n))))]))
(module+ main
(map Leonardo (range 25)) (map (curry Leonardo #:L0 0 #:L1 1 #:1+ 0) (range 25)))
(module+ test
(require rackunit) (check-equal? (Leonardo 0) 1) (check-equal? (Leonardo 1) 1) (check-equal? (Leonardo 2) 3) (check-equal? (Leonardo 3) 5))</lang>
- Output:
'(1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049) '(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368)
REXX
<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. */ @.=1 /*define the default for the @. array.*/ parse arg N L0 L1 a# . /*obtain optional arguments from the CL*/ if N == | N =="," then N= 25 /*Not specified? Then use the default.*/ if L0\== & L0\=="," then @.0= L0 /*Was " " " " value. */ if L1\== & L1\=="," then @.1= L1 /* " " " " " " */ if a#\== & a#\=="," then @.a= a# /* " " " " " " */ say 'The first ' N " Leonardo numbers are:" /*display a title for the output series*/ if @.0\==1 | @.1\==1 then say 'using ' @.0 " for L(0)" if @.0\==1 | @.1\==1 then say 'using ' @.1 " for L(1)" if @.a\==1 then say 'using ' @.a " for the add number" say /*display blank line before the output.*/ $= /*initialize the output line to "null".*/
do j=0 for N /*construct a list of Leonardo numbers.*/ if j<2 then z=@.j /*for the 1st two numbers, use the fiat*/ else do /*··· otherwise, compute the Leonardo #*/ _=@.0 /*save the old primary Leonardo number.*/ @.0=@.1 /*store the new primary number in old. */ @.1=@.0 + _ + @.a /*compute the next Leonardo number. */ z=@.1 /*store the next Leonardo number in Z. */ end /* [↑] only 2 Leonardo #s are stored. */ $=$ z /*append the just computed # to $ list.*/ end /*j*/ /* [↓] elide the leading blank in $. */
say strip($) /*stick a fork in it, we're all done. */</lang>
- output when using the default input:
The first 25 Leonardo numbers are: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
- output when using the input of: 12 0 1 0
The first 25 Leonardo numbers are: using 0 for L(0) using 1 for L(1) using 0 for the add number 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Ring
<lang ring>
- Project : Leonardo numbers
- Date : 2017/09/21
- Author : Gal Zsolt (~ CalmoSoft ~)
- Email : <calmosoft@gmail.com>
n0 = 1 n1 = 1 add = 1 see "" + n0 + " " + n1 for i=3 to 25
temp=n1 n1=n0+n1+add n0=temp see " "+ n1
next </lang> Output:
1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049
Ruby
Enumerators are nice for this. <lang ruby>def leonardo(l0=1, l1=1, add=1)
return to_enum(__method__,l0,l1,add) unless block_given? loop do yield l0 l0, l1 = l1, l0+l1+add end
end
p leonardo.take(25) p leonardo(0,1,0).take(25) </lang>
- Output:
[1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
Scala
<lang scala>def leo( n:Int, n1:Int=1, n2:Int=1, addnum:Int=1 ) : BigInt = n match {
case 0 => n1 case 1 => n2 case n => leo(n - 1, n1, n2, addnum) + leo(n - 2, n1, n2, addnum) + addnum
}
{ println( "The first 25 Leonardo Numbers:") (0 until 25) foreach { n => print( leo(n) + " " ) }
println( "\n\nThe first 25 Fibonacci Numbers:") (0 until 25) foreach { n => print( leo(n, n1=0, n2=1, addnum=0) + " " ) } } </lang>
- Output:
The first 25 Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
Sidef
<lang ruby>func 𝑳(n, 𝑳0 = 1, 𝑳1 = 1, 𝑳add = 1) {
{ (𝑳0, 𝑳1) = (𝑳1, 𝑳0 + 𝑳1 + 𝑳add) } * n return 𝑳0
}
say "The first 25 Leonardo numbers:" say 25.of { 𝑳(_) }
say "\nThe first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0:" say 25.of { 𝑳(_, 0, 1, 0) }</lang>
- Output:
The first 25 Leonardo numbers: [1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, 35421, 57313, 92735, 150049] The first 25 numbers using 𝑳0 of 0, 𝑳1 of 1, and adder of 0: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
VBA
<lang vb> Option Explicit
Private Sub LeonardoNumbers() Dim L, MyString As String
Debug.Print "First 25 Leonardo numbers :" L = Leo_Numbers(25, 1, 1, 1) MyString = Join(L, "; ") Debug.Print MyString Debug.Print "First 25 Leonardo numbers from 0, 1 with add number = 0" L = Leo_Numbers(25, 0, 1, 0) MyString = Join(L, "; ") Debug.Print MyString Debug.Print "If the first prarameter is too small :" L = Leo_Numbers(1, 0, 1, 0) MyString = Join(L, "; ") Debug.Print MyString
End Sub
Public Function Leo_Numbers(HowMany As Long, L_0 As Long, L_1 As Long, Add_Nb As Long) Dim N As Long, Ltemp
If HowMany > 1 Then ReDim Ltemp(HowMany - 1) Ltemp(0) = L_0: Ltemp(1) = L_1 For N = 2 To HowMany - 1 Ltemp(N) = Ltemp(N - 1) + Ltemp(N - 2) + Add_Nb Next N Else ReDim Ltemp(0) Ltemp(0) = "The first parameter is too small" End If Leo_Numbers = Ltemp
End Function </lang>
- Output:
First 25 Leonardo numbers : 1; 1; 3; 5; 9; 15; 25; 41; 67; 109; 177; 287; 465; 753; 1219; 1973; 3193; 5167; 8361; 13529; 21891; 35421; 57313; 92735; 150049 First 25 Leonardo numbers from 0, 1 with add number = 0 0; 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; 233; 377; 610; 987; 1597; 2584; 4181; 6765; 10946; 17711; 28657; 46368 If the first prarameter is too small : The first parameter is too small
zkl
<lang zkl>fcn leonardoNumber(n, n1=1,n2=1,addnum=1){
if(n==0) return(n1); if(n==1) return(n2); self.fcn(n-1,n1,n2,addnum) + self.fcn(n-2,n1,n2,addnum) + addnum
}</lang> <lang zkl>println("The first 25 Leonardo Numbers:"); foreach n in (25){ print(leonardoNumber(n)," ") } println("\n");
println("The first 25 Fibonacci Numbers:"); foreach n in (25){ print(leonardoNumber(n, 0,1,0)," ") } println();</lang>
- Output:
The first 25 Leonardo Numbers: 1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 35421 57313 92735 150049 The first 25 Fibonacci Numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368