CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: How do I write a recursive golden ratio?

1. Junior Member
Join Date
Feb 2020
Posts
1

## How do I write a recursive golden ratio?

This is my code to compute the golden ratio using recursion written in python but it is not returning me the output that i want. How should i change it?

def recursive_phi(n):
if n<0 or n == 0:
return 0
elif n == 1:
return 1
else:
return recursive_phi(n-1) + recursive_phi(n-2)
return recursive_phi(n)/recursive_phi(n-1)

recursive_fib(10)

2. ## Re: How do I write a recursive golden ratio?

What happens if n == 2?

3. Member +
Join Date
Feb 2017
Posts
514

## Re: How do I write a recursive golden ratio?

Originally Posted by raxer007
How should i change it?
It looks like you're trying to use the fact that you get the golden ratio if you divide two consecutive numbers in the Fibonacci sequence?

Since I don't know any Python I have to cheat and search internet,

https://www.programiz.com/python-pro...acci-recursion

Code:
```def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))```
This then will produce the golden ratio,

Code:
```def recursive_phi(n):
return(recur_fibo(n) / recur_fibo(n-1))```
If it works it proves Python is so easy you don't need any Python knowledge at all to use it.

4. ## Re: How do I write a recursive golden ratio?

If you just need the golden ratio then it can be calculated easily as

(1 + sqrt(5)) / 2

See https://www.mathsisfun.com/numbers/golden-ratio.html

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•