CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 1999
    Location
    Baytown, TX, United States
    Posts
    23

    I need a For...Next statement for a square root and show Fibonacci sequence

    I need a For...Next statement that will show the square roots of 2 to 12 and only the even numbers.

    I also need one that will show the first 10 numbers of the Fibonacci sequence.

    kazooie21

  2. #2
    Join Date
    Oct 1999
    Posts
    9

    Re: I need a For...Next statement for a square root and show Fibonacci sequence

    Here's a post from someone else, might help you understand how to approach the following

    1.
    Re: Fibonacci Numbers

    Let's look at what's happening. You have two previous values which
    you are adding to get the next value (your first two values are
    given of course). So what you need to do, is, in your For Next loop,
    store the values in two variables, say PrevVal1 and PrevVal2, as you
    loop through. Your new calculated number is passed to the variable
    of the previous value (in this case PrevVal2) and that variable's
    value is passed to the one before itself (PrevVal1). You also need to
    use some checks to determine which of the fibonacci numbers
    you're currently on to determine whether the result should be just 1
    or whether it's actually calculated.

    In pseudocode, it would be as follows:

    For CurrentFibNum = 0 to SOMEMAXIMUM

    If CurrentFibNum = 0 or CurrentFibNum = 1 then
    (Set the first two values in the first two loops)

    Set PrevVal1 = PrevVal2
    Set PrevVal2 = 1

    CurrentVal = 1

    Else
    (Calculate out the current value and shift the previous ones)

    Set CurrentVal = Prev1 + Prev2
    Set Prev1 = Prev2
    Set Prev2 = CurrentVal

    End If

    Display CurrentVal or store or whatever

    Loop


    That should at least give you a starting point. Good luck with it.

    2. I would approach the sqaure root question using modulus (mod) to determine whether the number is even, then perfrom sqaure root.


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