什么是斐波那契数列以及如何在 Python、C++ 和 JavaScript 中打印一个?

编程与谜题和数学密切相关。解决编程难题是一种让您保持精神活跃和健康的方法。它有助于培养解决问题的能力。

斐波那契数列问题是基于逻辑的编程问题之一,解决起来很有趣,也在技术面试中被问到。我们认为这是一个很好的项目,可以用您选择的任何语言磨练您的算术技能。

听起来不错?让我们开始吧。在本文中,您将学习如何打印最多 n 个项和 n 个值的斐波那契数列。

什么是斐波那契数列?

斐波那契数列是一系列数字,其中每个数字都是前两个数字的总和,从 0 和 1 开始。在数学中,此序列由 F n表示。

 F 0 = 0 and F 1 = 1.
and
F n = F n-1 + F n-2

斐波那契数列:

 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

打印前 n 个斐波那契数

问题陈述

你得到一个数字n 。您需要打印最多前n项的斐波那契数列。

示例 1 :让 n = 5。

前 5 个斐波那契数列:0 1 1 2 3

因此,输出为 0 1 1 2 3。

示例 2 :让 n = 7。

前 7 个斐波那契数列:0 1 1 2 3 5 8

因此,输出为 0 1 1 2 3 5 8。

打印前 n 个斐波那契数列的 C++ 程序

下面是打印前 n 个斐波那契数列的 C++ 程序:

 // C++ program to print the Fibonacci sequence upto n terms
#include <iostream>
using namespace std;
void printFibonacciSequence(int n)
{
int a = 0, b = 1;
int nextTerm;
if (n<1)
{
return;
}
cout << "Fibonacci Sequence Upto " << n << " terms:" << endl;
cout << a << " ";
for(int i=1; i<n; i++)
{
cout << b << " ";
// Next term is the sum of the last two terms
nextTerm = a + b;
a = b;
b = nextTerm;
}
cout << endl;
}
int main()
{
int n1 = 5;
printFibonacciSequence(n1);
int n2 = 7;
printFibonacciSequence(n2);
int n3 = 3;
printFibonacciSequence(n3);
int n4 = 10;
printFibonacciSequence(n4);
int n5 = 8;
printFibonacciSequence(n5);
return 0;
}

输出:

 Fibonacci Sequence Upto 5 terms:
0 1 1 2 3
Fibonacci Sequence Upto 7 terms:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 3 terms:
0 1 1
Fibonacci Sequence Upto 10 terms:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 8 terms:
0 1 1 2 3 5 8 13

打印前 n 个斐波那契数的 Python 程序

下面是打印前 n 个斐波那契数列的 Python 程序:

 # Python program to print the fibonacci sequence upto n terms
def printFibonacciSequence(n):
a = 0
b = 1
if (n < 1):
return
print("Fibonacci Sequence Upto", n, "terms:")
print(a, end=" ")
for i in range(1, n):
print(b, end=" ")
# Next term is the sum of the last two terms
nextTerm = a + b
a = b
b = nextTerm
print()

n1 = 5
printFibonacciSequence(n1)
n2 = 7
printFibonacciSequence(n2)
n3 = 3
printFibonacciSequence(n3)
n4 = 10
printFibonacciSequence(n4)
n5 = 8
printFibonacciSequence(n5)

输出:

 Fibonacci Sequence Upto 5 terms:
0 1 1 2 3
Fibonacci Sequence Upto 7 terms:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 3 terms:
0 1 1
Fibonacci Sequence Upto 10 terms:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 8 terms:
0 1 1 2 3 5 8 13

相关:如何在 C++、Python 和 JavaScript 中添加和减去两个矩阵

打印前 n 个斐波那契数列的 JavaScript 程序

下面是打印前 n 个斐波那契数列的 JavaScript 程序:

 // JavaScript program to print the Fibonacci sequence up to n terms
function printFibonacciSequence(n) {
let a = 0, b = 1;
let nextTerm;
if (n<1) {
return;
}
document.write("Fibonacci Sequence Upto " + n + " terms:" + "<br>");
document.write(a + " ");
for(let i=1; i<n; i++) {
document.write(b + " ");
// Next term is the sum of the last two terms
nextTerm = a + b;
a = b;
b = nextTerm;
}
document.write("<br>");
}

let n1 = 5;
printFibonacciSequence(n1);
let n2 = 7;
printFibonacciSequence(n2);
let n3 = 3;
printFibonacciSequence(n3);
let n4 = 10;
printFibonacciSequence(n4);
let n5 = 8;
printFibonacciSequence(n5);

输出:

 Fibonacci Sequence Upto 5 terms:
0 1 1 2 3
Fibonacci Sequence Upto 7 terms:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 3 terms:
0 1 1
Fibonacci Sequence Upto 10 terms:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 8 terms:
0 1 1 2 3 5 8 13

打印最多 n 值的斐波那契数列

问题陈述

你得到一个数字n 。您需要将斐波那契数列打印到小于或等于n的最接近值。

示例 1 :让 n = 38。

高达 38 的斐波那契数列:0 1 1 2 3 5 8 13 21 34

因此,输出为 0 1 1 2 3 5 8 13 21 34。

示例 2 :让 n = 91。

高达 91 的斐波那契数列:0 1 1 2 3 5 8 13 21 34 55 89

因此,输出为 0 1 1 2 3 5 8 13 21 34 55 89。

相关:如何使用递归求自然数之和

打印最多 n 值的斐波那契数列的 C++ 程序

下面是将斐波那契数列打印到 n 值的 C++ 程序:

 // C++ program to print the fibonacci sequence upto n value
#include <iostream>
using namespace std;
void printFibonacciSequence(int n)
{
int a = 0, b = 1;
int sum = 0;
cout << "Fibonacci Sequence Upto " << n << ":" << endl;
while(sum <= n)
{
cout << sum << " ";
a = b;
b = sum;
// Next term is the sum of the last two terms
sum = a + b;
}
cout << endl;
}
int main()
{
int n1 = 38;
printFibonacciSequence(n1);
int n2 = 56;
printFibonacciSequence(n2);
int n3 = 12;
printFibonacciSequence(n3);
int n4 = 91;
printFibonacciSequence(n4);
int n5 = 33;
printFibonacciSequence(n5);
return 0;
}

输出:

 Fibonacci Sequence Upto 38:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 56:
0 1 1 2 3 5 8 13 21 34 55
Fibonacci Sequence Upto 12:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 91:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Sequence Upto 33:
0 1 1 2 3 5 8 13 21

相关:如何在多种语言中找到两个数字的 LCM 和 GCD

打印最多 n 值的斐波那契数列的 Python 程序

下面是将斐波那契数列打印到 n 值的 Python 程序:

 # Python program to print the fibonacci sequence upto n value
def printFibonacciSequence(n):
a = 0
b = 1
sum = 0
print("Fibonacci Sequence Upto", n, ":")
while (sum<=n):
print(sum, end=" ")
a = b
b = sum
# Next term is the sum of the last two terms
sum = a + b
print()

n1 = 38
printFibonacciSequence(n1)
n2 = 56
printFibonacciSequence(n2)
n3 = 12
printFibonacciSequence(n3)
n4 = 91
printFibonacciSequence(n4)
n5 = 33
printFibonacciSequence(n5)

输出:

 Fibonacci Sequence Upto 38:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 56:
0 1 1 2 3 5 8 13 21 34 55
Fibonacci Sequence Upto 12:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 91:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Sequence Upto 33:
0 1 1 2 3 5 8 13 21

相关:如何使用 Python 创建和解码二维码

打印最多 n 值的斐波那契数列的 JavaScript 程序

下面是打印最多 n 值的斐波那契数列的 JavaScript 程序:

 // JavaScript program to print the fibonacci sequence upto n value
function printFibonacciSequence(n) {
let a = 0, b = 1;
let sum = 0;
document.write("Fibonacci Sequence Upto " + n + ":" + "<br>");
while(sum <= n)
{
document.write(sum + " ");
a = b;
b = sum;
// Next term is the sum of the last two terms
sum = a + b;
}
document.write("<br>");
}

let n1 = 38;
printFibonacciSequence(n1);
let n2 = 56;
printFibonacciSequence(n2);
let n3 = 12;
printFibonacciSequence(n3);
let n4 = 91;
printFibonacciSequence(n4);
let n5 = 33;
printFibonacciSequence(n5);

输出:

 Fibonacci Sequence Upto 38:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 56:
0 1 1 2 3 5 8 13 21 34 55
Fibonacci Sequence Upto 12:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 91:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Sequence Upto 33:
0 1 1 2 3 5 8 13 21

纠正你的编程错误

每个人在编程时都会犯错误。但是这些错误会导致很多问题。在编程时编写干净高效的代码非常重要。你怎么看?

你必须避免常见的编程错误,如重复代码、错误的变量名、不使用注释、语言过载、不备份代码、编写复杂代码、不提前计划、不提问等。纠正这些错误可以帮助你成为一个更好的程序员。