SlideShare uma empresa Scribd logo
1 de 35
Baixar para ler offline
Find n-th Fibonacci
iteratively
Illustrated walk through
public static long FindNthFibonacciIterative(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;
}
Let’s just get over with two special cases
when n == 0
[0]

[1]

[2]

[3]

[4]

[5]

[6]

0

1

?

?

?

?

?

if (n == 0) return 0;
if (n == 1) return 1;

Always return 0

when n == 1
[0]

[1]

[2]

[3]

[4]

[5]

[6]

0

1

?

?

?

?

?

if (n == 0) return 0;
if (n == 1) return 1;

Always return 1
[0]

[1]

[2]

[3]

[4]

[5]

[6]

0

1

?

?

?

?

?

fibNMinusTwo

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

fibN
when n == 6

0

fibNMinusTwo

1

?

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
0

fibNMinusTwo

1

0

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=2

0

fibNMinusTwo

1

0

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=2

0

fibNMinusTwo

1

0

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN

2 <= 6 is true
n == 6
i=2

0

+

fibNMinusTwo

1

1

?

?

fibNMinusOne

?

?

fibN

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

2nd Fibonacchi is 1
n == 6
i=2

1

fibNMinusTwo

1

1

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=2

1

fibNMinusTwo

1

1

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=3

1

fibNMinusTwo

1

1

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=3

1

fibNMinusTwo

1

1

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN

3 <= 6 is true
3ed fib.

n == 6
i=3

1

+

fibNMinusTwo

1

2

?

?

fibNMinusOne

?

?

fibN

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

3nd Fibonacchi is 2
n == 6
i=3

1

fibNMinusTwo

1

2

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=3

1

fibNMinusTwo

2

2

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=4

1

fibNMinusTwo

2

2

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=4

1

fibNMinusTwo

2

2

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN

4 <= 6 is true
n == 6
i=4

1

+

fibNMinusTwo

2

3

?

?

fibNMinusOne

?

?

fibN

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

4-th Fibonacchi is 3
n == 6
i=4

2

fibNMinusTwo

2

3

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=4

2

fibNMinusTwo

3

3

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=5

2

fibNMinusTwo

3

3

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=5

2

fibNMinusTwo

3

3

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN

5 <= 6 is true
n == 6
i=5

2

+

fibNMinusTwo

3

5

?

?

fibNMinusOne

?

?

fibN

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

5-th Fibonacchi is 5
n == 6
i=5

3

fibNMinusTwo

3

5

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=5

3

fibNMinusTwo

5

5

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=6

3

fibNMinusTwo

5

5

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=6

3

fibNMinusTwo

5

5

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN

6 <= 6 is true
n == 6
i=6

3

+

fibNMinusTwo

5

8

?

?

fibNMinusOne

?

?

fibN

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

6-th Fibonacchi is 8
n == 6
i=6

5

fibNMinusTwo

5

8

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=6

5

fibNMinusTwo

8

8

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=7

5

fibNMinusTwo

8

8

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN
n == 6
i=7

5

fibNMinusTwo

8

8

?

?

fibNMinusOne

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

?

?

fibN

7 <= 6 is false
n == 6
i=7

5

fibNMinusTwo

8

8

?

?

fibNMinusOne

?

?

fibN

long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

Exit the for loop
n == 6
i=7

5

fibNMinusTwo

8

8

?

?

fibNMinusOne

?

?

fibN

Return fibN = 8
long fibNMinusTwo = 0;
long fibNMinusOne = 1;
long fibN = 0;
6-th Fibonacchi is 8
for (int i = 2; i <= n; ++i)
{
fibN = fibNMinusOne + fibNMinusTwo;
fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}
return fibN;

Mais conteúdo relacionado

Último

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 

Último (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Find n th fibonacci iteratively - illustrated walkthrough

  • 2. public static long FindNthFibonacciIterative(int n) { if (n == 0) return 0; if (n == 1) return 1; long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; }
  • 3. Let’s just get over with two special cases when n == 0 [0] [1] [2] [3] [4] [5] [6] 0 1 ? ? ? ? ? if (n == 0) return 0; if (n == 1) return 1; Always return 0 when n == 1 [0] [1] [2] [3] [4] [5] [6] 0 1 ? ? ? ? ? if (n == 0) return 0; if (n == 1) return 1; Always return 1
  • 4. [0] [1] [2] [3] [4] [5] [6] 0 1 ? ? ? ? ? fibNMinusTwo fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; fibN
  • 5. when n == 6 0 fibNMinusTwo 1 ? ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 6. 0 fibNMinusTwo 1 0 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 7. n == 6 i=2 0 fibNMinusTwo 1 0 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 8. n == 6 i=2 0 fibNMinusTwo 1 0 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 2 <= 6 is true
  • 9. n == 6 i=2 0 + fibNMinusTwo 1 1 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 2nd Fibonacchi is 1
  • 10. n == 6 i=2 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 11. n == 6 i=2 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 12. n == 6 i=3 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 13. n == 6 i=3 1 fibNMinusTwo 1 1 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 3 <= 6 is true
  • 14. 3ed fib. n == 6 i=3 1 + fibNMinusTwo 1 2 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 3nd Fibonacchi is 2
  • 15. n == 6 i=3 1 fibNMinusTwo 1 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 16. n == 6 i=3 1 fibNMinusTwo 2 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 17. n == 6 i=4 1 fibNMinusTwo 2 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 18. n == 6 i=4 1 fibNMinusTwo 2 2 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 4 <= 6 is true
  • 19. n == 6 i=4 1 + fibNMinusTwo 2 3 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 4-th Fibonacchi is 3
  • 20. n == 6 i=4 2 fibNMinusTwo 2 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 21. n == 6 i=4 2 fibNMinusTwo 3 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 22. n == 6 i=5 2 fibNMinusTwo 3 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 23. n == 6 i=5 2 fibNMinusTwo 3 3 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 5 <= 6 is true
  • 24. n == 6 i=5 2 + fibNMinusTwo 3 5 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 5-th Fibonacchi is 5
  • 25. n == 6 i=5 3 fibNMinusTwo 3 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 26. n == 6 i=5 3 fibNMinusTwo 5 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 27. n == 6 i=6 3 fibNMinusTwo 5 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 28. n == 6 i=6 3 fibNMinusTwo 5 5 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 6 <= 6 is true
  • 29. n == 6 i=6 3 + fibNMinusTwo 5 8 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; 6-th Fibonacchi is 8
  • 30. n == 6 i=6 5 fibNMinusTwo 5 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 31. n == 6 i=6 5 fibNMinusTwo 8 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 32. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN
  • 33. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; ? ? fibN 7 <= 6 is false
  • 34. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne ? ? fibN long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN; Exit the for loop
  • 35. n == 6 i=7 5 fibNMinusTwo 8 8 ? ? fibNMinusOne ? ? fibN Return fibN = 8 long fibNMinusTwo = 0; long fibNMinusOne = 1; long fibN = 0; 6-th Fibonacchi is 8 for (int i = 2; i <= n; ++i) { fibN = fibNMinusOne + fibNMinusTwo; fibNMinusTwo = fibNMinusOne; fibNMinusOne = fibN; } return fibN;