Posted on by Kalkicode
Code Number

# Pell Number

The Pell number sequence is a series of numbers that follows a specific pattern. Each number in the sequence is the sum of twice the previous number and the number before it. The first few numbers in the Pell series are 0, 1, 2, 5, 12, 29, 70, 169, 408, 985, and so on. In this article, we will discuss the Pell number series, provide an explanation of the problem, present a suitable example, outline the algorithm and pseudocode with explanations, and analyze the resultant output.

## Problem Explanation

The problem is to generate the Pell number series up to a given size. The size refers to the number of elements in the series that need to be generated. For example, if the size is 10, we need to generate the first 10 numbers in the Pell series.

## Example

Let's consider an example where we want to generate the Pell number series up to size 10. The expected output for this example would be: 0, 1, 2, 5, 12, 29, 70, 169, 408, 985.

## Algorithm and Pseudocode

To solve this problem, we can use a simple iterative approach. Here is the algorithm to generate the Pell number series:

1. Initialize three variables: a = 1, b = 0, c = 0.
2. Iterate 'size' number of times:
• Calculate the next Pell number using the formula: c = 2 * b + a.
• Update the values of a and b: a = b and b = c.
• Print the current Pell number.
The pseudocode representation of the algorithm is as follows:
```function pell_numbers(size):
a = 1
b = 0
c = 0
for i from 0 to size:
c = 2 * b + a
a = b
b = c
print c
```

## Code Solution

Here given code implementation process.

``````/*
C Program
+ Print Pell Number Series
*/
#include<stdio.h>

//Display pell number in given size
void pell_numbers(int size)
{

//Setup initial value
int a = 1;
int b = 0;
int c = 0;

for (int i = 0; i < size; ++i)
{
//Get pell number
c = 2 * b + a;
a = b;
b = c;
printf("%d  ",c );
}

}
int main(){

//Test Case
pell_numbers(10);

return 0;
}```
```

#### Output

``1  2  5  12  29  70  169  408  985  2378``
``````/*
C++ Program
Print Pell Number Series
*/
#include<iostream>

using namespace std;

class MyNumber {
public:

//Display pell number in given size
void pell_numbers(int size) {
//Setup initial value
int a = 1;
int b = 0;
int c = 0;
for (int i = 0; i < size; ++i) {
//Get pell number
c = 2 *b + a;
a = b;
b = c;
cout << " " << c;
}
}
};
int main() {
MyNumber obj;
obj.pell_numbers(10);
return 0;
}```
```

#### Output

`` 1 2 5 12 29 70 169 408 985 2378``
``````/*
Java Program
Print Pell Number Series
*/
public class MyNumber {

//Display pell number in given size
public void pell_numbers(int size)
{

//Setup initial value
int a = 1;
int b = 0;
int c = 0;

for (int i = 0; i < size; ++i)
{
//Get pell number
c = 2 * b + a;
a = b;
b = c;
System.out.print("  "+c );
}

}

public static void main(String[] args) {

MyNumber obj = new MyNumber();

obj.pell_numbers(10);
}
}```
```

#### Output

`` 1 2 5 12 29 70 169 408 985 2378``
``````/*
C# Program
Print Pell Number Series
*/
using System;
public class MyNumber {

//Display pell number in given size
public void pell_numbers(int size) {

//Setup initial value
int a = 1;
int b = 0;
int c = 0;

for (int i = 0; i < size; ++i) {
//Get pell number
c = 2 * b + a;
a = b;
b = c;
Console.Write("  " + c);
}

}

public static void Main(String[] args) {

MyNumber obj = new MyNumber();

obj.pell_numbers(10);
}
}```
```

#### Output

``  1  2  5  12  29  70  169  408  985  2378``
``````<?php
/*
Php Program
Print Pell Number Series
*/
class MyNumber {
//Display pell number in given size

public  function pell_numbers(\$size) {
//Setup initial value
\$a = 1;
\$b = 0;
\$c = 0;
for (\$i = 0; \$i < \$size; ++\$i) {
//Get pell number
\$c = 2 *\$b + \$a;
\$a = \$b;
\$b = \$c;
echo(" ". \$c);
}
}
};

function main() {
\$obj = new MyNumber();
\$obj->pell_numbers(10);
}
main();```
```

#### Output

`` 1 2 5 12 29 70 169 408 985 2378``
``````/*
Node Js Program
Print Pell Number Series
*/
class MyNumber {
//Display pell number in given size
pell_numbers(size) {
//Setup initial value
var a = 1;
var b = 0;
var c = 0;
for (var i = 0; i < size; ++i) {
//Get pell number
c = 2 *b + a;
a = b;
b = c;
process.stdout.write(" " + c);
}
}
}

function main(args) {
var obj = new MyNumber();
obj.pell_numbers(10);
}
main();```
```

#### Output

`` 1 2 5 12 29 70 169 408 985 2378``
``````# Python 3 Program
# Print Pell Number Series
class MyNumber :
# Display pell number in given size
def pell_numbers(self, size) :
# Setup initial value
a = 1
b = 0
c = 0
i = 0
while (i < size) :
# Get pell number
c = 2 * b + a
a = b
b = c
print(" ", c,end="")
i += 1

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

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

#### Output

`` 1  2  5  12  29  70  169  408  985  2378``
``````# Ruby Program

# Print Pell Number Series
class MyNumber
# Display pell number in given size
def pell_numbers(size)
# Setup initial value
a = 1
b = 0
c = 0
i = 0
while (i < size)
# Get pell number
c = 2 * b + a
a = b
b = c
print(" ", c)
i += 1
end
end
end
def main()
obj = MyNumber.new()
obj.pell_numbers(10)
end
main()```
```

#### Output

`` 1 2 5 12 29 70 169 408 985 2378``
``````/*
Scala Program
Print Pell Number Series
*/
class MyNumber {
//Display pell number in given size
def pell_numbers(size: Int): Unit = {
//Setup initial value
var a: Int = 1;
var b: Int = 0;
var c: Int = 0;
var i: Int = 0;
while (i < size) {
//Get pell number
c = 2 * b + a;
a = b;
b = c;
print(" " + c);
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
obj.pell_numbers(10);
}
}```
```

#### Output

`` 1 2 5 12 29 70 169 408 985 2378``
``````/*
Swift 4 Program
Print Pell Number Series
*/
class MyNumber {
//Display pell number in given size
func pell_numbers(_ size: Int) {
//Setup initial value
var a: Int = 1;
var b: Int = 0;
var c: Int = 0;
var i: Int = 0;
while (i < size) {
//Get pell number
c = 2 * b + a;
a = b;
b = c;
print(" ", c,terminator:"");
i += 1;
}
}
}
func main() {
let obj: MyNumber = MyNumber();
obj.pell_numbers(10);
}
main();```
```

#### Output

``  1  2  5  12  29  70  169  408  985  2378``

## Resultant Output Explanation

When we run the provided code with the given test case (pell_numbers(10)), it will generate the first 10 numbers in the Pell number series. The output will be as follows: 0, 1, 2, 5, 12, 29, 70, 169, 408, 985.

## Time Complexity

The time complexity of this algorithm is O(size), where 'size' is the number of elements to be generated in the Pell series. Since we are iterating 'size' number of times, the time taken to generate the series will be proportional to the size.

## 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