Fibonacci sequence (implemented in JavaScript and Python)

Source: Internet
Author: User

1. Using JavaScript

What is the nth number of Fibonacci sequences?

// Requirement: Encapsulates a function to find the nth term of the Fibonacci sequence

Fibonacci sequence
var n=parseint (Prompt ("Enter the number of Fibonacci sequences you want to Know");
document.write (f (n));
function f (n) {
if (n>=3) {
var a=1;
var b=1;
for (Var i=3;i<=n;i++) {
var temp=b;
B=a+b;
A=temp;
}
return b;
}
else if (n<=2) {
Return 1
}

}

2. Using the Python method
 whileTrue:#Implement the function of finding the Fibonacci sequence to n items multiple timesN=int (Input ("Please enter an integer:"))    if0<n<=2:#1th, 2 digits is 1        Print("Fibonacci Sequence No."N"items are: 1")        Print("To exit, press: 0")#prompts the user to press 0 to exit    elifN>=3:#N At the beginning of the 3rd bitA = 1b= 1F=0 I=3 while3<=i<=n:f= A +B a=b b=F i+=1Print("Fibonacci Sequence No."N"items are:", B)Print("To exit, press: 0")    elifN==0:#Press 0 to exit at any timeExit ()

Fibonacci sequence (implemented in JavaScript and Python)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.