Results 1 to 2 of 2

Math Help - Algorithm

  1. #1
    Member
    Joined
    Feb 2008
    Posts
    79

    Algorithm

    This is in my math class, but I'm horrible at programming so don't quite get it. But, for a, b and c below, I have to find "b" (all values- note that b is an integer) that'll make the algorithm print "Hello".

    NOTE: the ....... below is in place of the indentation.. wouldn't let me indent it so I used .'s to make the space.

    a.)

    k := b
    While k < 5
    ........k := 2k - 1
    End While
    Print "Hello"

    b.) Note for this one =! means not equal

    k := b
    While k =! 5
    ........k := 2k - 1
    End While
    Print "Hello"

    c.)

    k := b
    While k < 5
    ........k := 2k + 1
    End While
    Print "Hello"

    My teacher said another way to ask the question of finding b that'd make it print "Hello" is to find all values of b which will NOT result in an infinite loop.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member Aryth's Avatar
    Joined
    Feb 2007
    From
    USA
    Posts
    652
    Thanks
    2
    Awards
    1
    \text{a) You have: }
    k:= b

    \text{while }k < 5

    \text{        }k := 2k - 1

    \text{End While}

    \text{Print "Hello"}

    The values that would print Hello would be the values that make it through the loop, so 2 < b < 5 so that the loop is executed and ended and all b > 5 so that the loop is overlooked.

    \text{b) You have: }
    k:= b

    \text{while }k =! 5

    \text{        }k := 2k - 1

    \text{End While}

    \text{Print "Hello"}

    Obviously, b = 5 would print hello, also, b = 3 would print hello.

    5 := 2k - 1

    6 := 2k

    3 := k := b

    So, the answers are 5 and 3.

    \text{c) You have: }
    k:= b

    \text{while }k < 5

    \text{        }k := 2k + 1

    \text{End While}

    \text{Print "Hello"}

    The answer is:

    \text{The program will print Hello }\forall \ b \in \mathbb{N}\text{ including 0.}
    Last edited by Aryth; February 27th 2008 at 01:18 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. algorithm
    Posted in the Advanced Applied Math Forum
    Replies: 3
    Last Post: January 19th 2010, 02:46 AM
  2. Algorithm
    Posted in the Advanced Math Topics Forum
    Replies: 7
    Last Post: November 22nd 2009, 07:11 AM
  3. Algorithm help
    Posted in the Discrete Math Forum
    Replies: 6
    Last Post: November 12th 2009, 04:10 AM
  4. algorithm
    Posted in the Math Topics Forum
    Replies: 2
    Last Post: July 16th 2008, 01:29 PM
  5. gcd algorithm
    Posted in the Discrete Math Forum
    Replies: 1
    Last Post: December 4th 2007, 11:47 PM

Search Tags


/mathhelpforum @mathhelpforum