SlideShare a Scribd company logo
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;

More Related Content

Recently uploaded

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀DianaGray10
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Frank van Harmelen
 

Recently uploaded (20)

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 

Featured

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
 

Featured (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;