Posted on by Kalkicode
Code Number

# Lucas number

The Lucas numbers are a series of integers that form a sequence similar to the Fibonacci sequence. The sequence starts with 2 and 1, and each subsequent number is the sum of the previous two numbers. In this article, we will explore how to generate the first 'N' Lucas numbers and provide a C code implementation to print them.

## Problem Statement

Given an integer 'N', the task is to find and print the first 'N' Lucas numbers.

## Example

Let's take 'N' as 10. The first 10 Lucas numbers are: 2, 1, 3, 4, 7, 11, 18, 29, 47, 76.

## Pseudocode and Algorithm

``````// Function to generate and print the first 'N' Lucas numbers
function lucas_numbers(N)
// Initialize variables for Lucas numbers
a <- 2
b <- 1

// Print initial message
print "N initial Lucas numbers are: "

// Loop to generate 'N' Lucas numbers
for i from 1 to N do
// Print the current Lucas number 'a'
print a

// Calculate the next Lucas number 'c' by adding 'a' and 'b'
c <- a + b

// Update 'a' to the value of 'b'
a <- b

// Update 'b' to the value of 'c'
b <- c

// End of loop
end for

// End of function
end function
``````

To generate the first 'N' Lucas numbers, we will use a simple iterative approach. We will initialize two variables 'a' and 'b' with the first two Lucas numbers, which are 2 and 1. Then, we will loop 'N' times, printing the current value of 'a' in each iteration and updating 'a' and 'b' to the next Lucas number in the sequence.

1. Start with 'N', the number of Lucas numbers to be generated, and set 'a' to 2 and 'b' to 1.
2. Print "N initial Lucas numbers are: ".
3. Loop from 0 to N-1: a. Print the value of 'a'. b. Calculate the next Lucas number, 'c', by adding 'a' and 'b'. c. Update 'a' to the value of 'b'. d. Update 'b' to the value of 'c'.
4. End of loop.

## Code Solution

Here given code implementation process.

``````//C Program
//Print lucas numbers list
#include <stdio.h>

//function which are display  initial N lucas number
void lucas_no(int n)
{
//Variable which are control execution
//set initial values
int a = 2;
int b = 1;
int c = 0;

printf("\n%d initial lucas number is \n",n);
//loop which are printing N lucas numbers
for (int i = 0; i < n; i++)
{
printf("%d ",a );
c = a + b;
//next lucas no
a = b;
b = c;
}
}

int main() {

lucas_no(10);
return 0;
}```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````/*
C++ Program
Print lucas numbers list
*/
#include<iostream>

using namespace std;

class MyNumber {
public:

//Function which are display  initial N lucas number
void lucas_no(int n) {
//Variable which are control execution
//set initial values
int a = 2;
int b = 1;
int c = 0;
cout << "\n" << n << " initial lucas number is \n";
//loop which are printing N lucas numbers

for (int i = 0; i < n; i++) {
cout << " " << a;
c = a + b;
//next lucas no
a = b;
b = c;
}
}
};
int main() {
MyNumber obj;
// Test Case
obj.lucas_no(10);
return 0;
}```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````/*
Java Program
Print lucas numbers list
*/

public class MyNumber {

//Function which are display  initial N lucas number
public void lucas_no(int n)
{
//Variable which are control execution
//set initial values
int a = 2;
int b = 1;
int c = 0;

System.out.print("\n"+n+" initial lucas number is \n");
//loop which are printing N lucas numbers
for (int i = 0; i < n; i++)
{
System.out.print(" "+a );
c = a + b;
//next lucas no
a = b;
b = c;
}

}

public static void main(String[] args) {

MyNumber obj = new MyNumber();
// Test Case
obj.lucas_no(10);
}
}```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````/*
C# Program
Print lucas numbers list
*/
using System;
public class MyNumber {

//Function which are display  initial N lucas number
public void lucas_no(int n) {
//Variable which are control execution
//set initial values
int a = 2;
int b = 1;
int c = 0;

Console.Write("\n" + n + " initial lucas number is \n");
//loop which are printing N lucas numbers
for (int i = 0; i < n; i++) {
Console.Write(" " + a);
c = a + b;
//next lucas no
a = b;
b = c;
}

}

public static void Main(String[] args) {

MyNumber obj = new MyNumber();
// Test Case
obj.lucas_no(10);
}
}```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````# Python 3 Program
# Print lucas numbers list
class MyNumber :
#Function which are display  initial N lucas number
def lucas_no(self, n) :
#set initial values
#Variable which are control execution
a = 2
b = 1
c = 0
print("\n", n ," initial lucas number is ")
#loop which are printing N lucas numbers
i = 0
while (i < n) :
print(" ", a)
c = a + b
#next lucas no
a = b
b = c
i += 1

def main() :
obj = MyNumber()
obj.lucas_no(10)

if __name__ == "__main__":
main()```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````# Ruby Program
# Print lucas numbers list
class MyNumber
#Function which are display  initial N lucas number
def lucas_no(n)

#Variable which are control execution
#Set initial values
a = 2
b = 1
c = 0
print("\n", n ," initial lucas number is \n")
#loop which are printing N lucas numbers
i = 0
while (i < n)
print(" ", a)
c = a + b
#next lucas no
a = b
b = c
i += 1
end
end
end
def main()
obj = MyNumber.new()
obj.lucas_no(10)
end
main()```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````/*
Scala Program
Print lucas numbers list
*/
class MyNumber {
//Function which are display  initial N lucas number
def lucas_no(n: Int): Unit = {
//Variable which are control execution
//set initial values
var a: Int = 2;
var b: Int = 1;
var c: Int = 0;
print(s"\n \$n initial lucas number is \n");
//loop which are printing N lucas numbers
var i: Int = 0;
while (i < n) {
print(s" \$a");
c = a + b;
//next lucas no
a = b;
b = c;
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
obj.lucas_no(10);
}
}```
```

#### Output

`````` 10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````/*
Swift 4 Program
Print lucas numbers list
*/
class MyNumber {
//Function which are display  initial N lucas number
func lucas_no(_ n: Int) {
//Variable which are control execution
//set initial values
var a: Int = 2;
var b: Int = 1;
var c: Int = 0;
print("\n", n ," initial lucas number is ");
//loop which are printing N lucas numbers
var i: Int = 0;
while (i < n) {
print(" ", a,terminator:"");
c = a + b;
//next lucas no
a = b;
b = c;
i += 1;
}
}
}
func main() {
let obj: MyNumber = MyNumber();
obj.lucas_no(10);
}
main();```
```

#### Output

`````` 10  initial lucas number is
2  1  3  4  7  11  18  29  47  76``````
``````<?php
/*
Php Program
Print lucas numbers list
*/
class MyNumber {
//Function which are display  initial N lucas number

public  function lucas_no(\$n) {
//Variable which are control execution
//set initial values
\$a = 2;
\$b = 1;
\$c = 0;
echo("\n". \$n ." initial lucas number is \n");
//loop which are printing N lucas numbers

for (\$i = 0; \$i < \$n; \$i++) {
echo(" ". \$a);
\$c = \$a + \$b;
//next lucas no
\$a = \$b;
\$b = \$c;
}
}
};

function main() {
\$obj = new MyNumber();
// Test Case

\$obj->lucas_no(10);
}
main();```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````
``````/*
Node Js Program
Print lucas numbers list
*/
class MyNumber {
//Function which are display  initial N lucas number
lucas_no(n) {
//Variable which are control execution
//set initial values
var a = 2;
var b = 1;
var c = 0;
process.stdout.write("\n" + n + " initial lucas number is \n");
//loop which are printing N lucas numbers

for (var i = 0; i < n; i++) {
process.stdout.write(" " + a);
c = a + b;
//next lucas no
a = b;
b = c;
}
}
}

function main(args) {
var obj = new MyNumber();
// Test Case
obj.lucas_no(10)
}
main();```
```

#### Output

``````10 initial lucas number is
2 1 3 4 7 11 18 29 47 76``````

## Explanation

• We define the function `lucas_no` that takes an integer 'n' as input.
• Inside the function, we initialize variables 'a', 'b', and 'c' to 2, 1, and 0, respectively.
• We then use a loop to generate 'n' Lucas numbers. In each iteration, we print the current value of 'a' and update 'a', 'b', and 'c' to generate the next Lucas number.
• After the loop, we print a newline to separate the output from any other text that may follow.

## Output Explanation

When we run the code with 'n' set to 10, it prints the first 10 Lucas numbers as follows:

``10 initial Lucas numbers are: 2 1 3 4 7 11 18 29 47 76``

## Time Complexity

The time complexity of this implementation is O(N) since we iterate 'N' times to generate the first 'N' Lucas numbers. The calculation of each Lucas number requires constant time operations, so the overall time complexity is linear in terms of 'N'.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post