# Algorithm

• Feb 27th 2008, 12:39 PM
DiscreteW
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.
• Feb 27th 2008, 01:02 PM
Aryth
$\displaystyle \text{a) You have: }$
$\displaystyle k:= b$

$\displaystyle \text{while }k < 5$

$\displaystyle \text{ }k := 2k - 1$

$\displaystyle \text{End While}$

$\displaystyle \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.

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

$\displaystyle \text{while }k =! 5$

$\displaystyle \text{ }k := 2k - 1$

$\displaystyle \text{End While}$

$\displaystyle \text{Print "Hello"}$

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

$\displaystyle 5 := 2k - 1$

$\displaystyle 6 := 2k$

$\displaystyle 3 := k := b$

So, the answers are 5 and 3.

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

$\displaystyle \text{while }k < 5$

$\displaystyle \text{ }k := 2k + 1$

$\displaystyle \text{End While}$

$\displaystyle \text{Print "Hello"}$

$\displaystyle \text{The program will print Hello }\forall \ b \in \mathbb{N}\text{ including 0.}$