{"id":12639,"date":"2021-10-01T11:00:58","date_gmt":"2021-10-01T05:30:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=12639"},"modified":"2021-11-22T18:33:26","modified_gmt":"2021-11-22T13:03:26","slug":"python-program-to-generate-first-n-numbers-of-pell-series","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-first-n-numbers-of-pell-series\/","title":{"rendered":"Python Program to Generate first N numbers of Pell Series"},"content":{"rendered":"
Given the number N, the task is to print the first n numbers of the pell series in Python.<\/p>\n
Pell Series:<\/strong><\/p>\n The Pell Series is a series in which the next number equals the sum of twice the previous number and the number preceding the previous number.<\/p>\n It begins with 1 and 2.<\/p>\n Pell Numbers: 1,2,5,12,29,70,169,…<\/p>\n Consider the following number sequence and try to match it to the definition of the Pell Series:<\/p>\n 1 + 2 x 2 = 5<\/p>\n 2 + 5* 2 = 12<\/p>\n 5 + 12 x 2 = 29<\/p>\n 12 + 29*2 = 70<\/p>\n 29 + 70*2 = 169<\/p>\n and so on\u2026<\/p><\/blockquote>\n Now that you have a clear idea of what the Pell Series are, let\u2019s move on to how we shall write the Python code to generate the same.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to generate the first N number of the pell series in Python.<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given the number N, the task is to print the first n numbers of the pell series in Python. Pell Series: The Pell Series is a series in which the next number equals the sum of twice the previous number and the number preceding the previous number. It begins with 1 and 2. Pell Numbers: …<\/p>\nGiven number = 12<\/pre>\n
The first 12 numbers of the pell series are :\r\n1 2 5 12 29 70 169 408 985 2378 5741 13860<\/pre>\n
Given number = 21<\/pre>\n
The first 21 numbers of the pell series are :\r\n1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109\r\n 15994428 38613965<\/pre>\n
Program to Generate first N numbers of Pell Series in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\nnumb = 21\r\n# Take a variable temp1 and initialize it to 1.\r\ntemp1 = 1\r\n# Take another variable temp2 and initialize it to 2.\r\ntemp2 = 2\r\n# Take the third variable temp3 and initialize it to 0.\r\ntemp3 = 0\r\n# Now, print the first two numbers in the series,\r\n# and we'll print the other generated numbers in the order they were generated.\r\nprint('The first', numb, 'numbers of the pell series are :')\r\nprint(temp1, temp2, end=' ')\r\n# Loop till number -2 using For loop.\r\nfor i in range(numb-2):\r\n # Add the value of temp1 and twice the temp2 value\r\n # and initialize the temp3 with this value.\r\n temp3 = temp1+2*temp2\r\n # Swap the previous number for the previous number's predecessor (temp1 =temp2).\r\n temp1 = temp2\r\n # Swap the generated and previously generated numbers (temp2 = temp3)\r\n temp2 = temp3\r\n # Print the value of temp3.\r\n print(temp3, end=' ')\r\n<\/pre>\n
The first 21 numbers of the pell series are :\r\n1 2 5 12 29 70 169 408 985 2378 5741 13860 33461 80782 195025 470832 1136689 2744210 6625109\r\n 15994428 38613965<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the number N as user input using int(input()) and store it in a variable.\r\nnumb = int(input('Enter some random number = '))\r\n# Take a variable temp1 and initialize it to 1.\r\ntemp1 = 1\r\n# Take another variable temp2 and initialize it to 2.\r\ntemp2 = 2\r\n# Take the third variable temp3 and initialize it to 0.\r\ntemp3 = 0\r\n# Now, print the first two numbers in the series,\r\n# and we'll print the other generated numbers in the order they were generated.\r\nprint('The first', numb, 'numbers of the pell series are :')\r\nprint(temp1, temp2, end=' ')\r\n# Loop till number -2 using For loop.\r\nfor i in range(numb-2):\r\n # Add the value of temp1 and twice the temp2 value\r\n # and initialize the temp3 with this value.\r\n temp3 = temp1+2*temp2\r\n # Swap the previous number for the previous number's predecessor (temp1 =temp2).\r\n temp1 = temp2\r\n # Swap the generated and previously generated numbers (temp2 = temp3)\r\n temp2 = temp3\r\n # Print the value of temp3.\r\n print(temp3, end=' ')\r\n<\/pre>\n
Enter some random number = 12\r\nThe first 12 numbers of the pell series are :\r\n1 2 5 12 29 70 169 408 985 2378 5741 13860<\/pre>\n
\n