CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    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. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,406

    Re: How do I write a recursive golden ratio?

    What happens if n == 2?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: How do I write a recursive golden ratio?

    Quote Originally Posted by raxer007 View Post
    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. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,833

    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
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2023
    Posts
    1

    Re: How do I write a recursive golden ratio?

    Exploring the elegance of recursive algorithms, crafting a recursive golden ratio is a mesmerizing journey into the harmonious world of mathematics. It's akin to composing a symphony of numbers, each iteration revealing a deeper layer of beauty. Embracing the Fibonacci sequence and the divine proportion, the recursive golden ratio becomes a poetic dance of numbers converging towards perfection. The recursive approach adds a layer of complexity, elevating the challenge and the joy of unraveling its mysteries. As you embark on this intellectual adventure, remember that every line of code is a stroke on the canvas of your creativity.
    Last edited by WandaAZ; January 17th, 2024 at 07:19 AM.

  6. #6
    Join Date
    Jul 2024
    Posts
    3

    Re: How do I write a recursive golden ratio?

    I think it will look like:
    def recursive_phi(n):
    if n < 0:
    return 0
    elif n == 0:
    return 0
    elif n == 1:
    return 1
    else:
    return recursive_phi(n-1) + recursive_phi(n-2)

    def golden_ratio(n):
    return recursive_phi(n) / recursive_phi(n-1)

    print(golden_ratio(10)) the decision did not come to me immediately, it did not come immediately. It came after my studies personal statement residency writing service It was very appropriate, because it was a kind of help for me ..
    Last edited by vinston; July 16th, 2024 at 10:52 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured