
-----------------------------------
deworld
Fri Nov 06, 2015 5:28 pm

I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
What is it you are trying to achieve?
To make a program that uses loops to do the collatzconjecture: divide even numbers by 2 and odd multiply by 3 add 1 
User can input whatever natural number they wish
Must be bulletproof 
and user must be able to repeat the program

What is the problem you are having?
I can't seem to use an if statement to confirm if its a even number or not to then div 2 or multiply 3 add 1 then div 2
It keeps giving me a console that doesn't finish

Describe what you have tried to solve this problem
i have tried to experiment with loops inside ifs to repeat the div but with no success 


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)





var input : int
var strin, input2 : string

put "Enter any natural number"
loop
    get strin
    exit when strintok (strin)
    put "Not a natural number." ..
end loop
input := strint (strin)
loop
    loop
        if input = 0 then
            put "This is not a natural number. Rlease renter a number ABOVE 0"
            get input
        elsif input < 0 then
            put "This is too low. Please renter a number ABOVE 0"
            get input
        end if
        exit when input >= 1
    end loop

    loop
        if input div 2 = 1 then
            input := input div 2
            put input
        elsif (input * 3 + 1) div 2 = 1 then
            input := (input * 3 + 1) div 2
            put input
        end if
        exit when input = 1
    end loop
    exit when input = 1
end loop






Please specify what version of Turing you are using
4.1.1

-----------------------------------
Zren
Fri Nov 06, 2015 5:53 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
I think you're mixing up div is used to count how many time the right side can go into the left side. Eg: 11 div 2 expands to 5 since 2 goes into 11 five times, with a remainder of 1 (think of long division).

no remainder.

As an aside, try to only use get input in one location so you can keep all your user validation code in a single place. Right now your program isn't bulletproof if I enter asdfasdf after getting the error "This is too low. Please renter a number ABOVE 0".

-----------------------------------
Insectoid
Fri Nov 06, 2015 5:57 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
mod command.

-----------------------------------
deworld
Fri Nov 06, 2015 6:24 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
I'm not sure whats wrong i've done what i could to clean it up but I cant find out whats making it so it cant output it.


On a side note i have made it so the input in the loop only has 1 get input so i bulletproofed it so you cant write letters made 2 get inputs because i'm not sure how to combine both of them into 1 input.

-----------------------------------
Insectoid
Fri Nov 06, 2015 6:41 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
You already have code that checks if the input is a number and converts it or gives an error as required. If you need to get new input from the user, just run that code again. You could make that code a procedure, or you could put it inside another loop that restarts when you detect that the number is unnatural.

-----------------------------------
deworld
Fri Nov 06, 2015 6:51 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
so something like this?
loop

    loop
        get strin
        exit when strintok (strin)
        put "Not a natural number. Please try again: " ..
    end loop
    input := strint (strin)

    if input = 0 then
        put "This is not a natural number. Rlease renter a number ABOVE 0"
    elsif input < 0 then
        put "This is too low. Please renter a number ABOVE 0"
    end if
    exit when input >= 1

end loop

-----------------------------------
Insectoid
Fri Nov 06, 2015 7:28 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
Does it work?

-----------------------------------
deworld
Fri Nov 06, 2015 9:35 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
yup i finnaly got it to work let me know if you catch anything i missed/did wrong

var input : int
var cont : string 

var strin : string 

loop
    put "Enter any natural number" 
    loop

        loop
            get strin 
            exit when strintok (strin)
            put "Not a number. Please try again: " ..
        end loop
        input := strint (strin)
        if input < 1 then
            put "This is not a natural number(number ABOVE 0). Rlease renter a number: " ..
            end if
        exit when input >= 1

    end loop

    loop 
        exit when input = 1 %exit 
        if input mod 2 = 0 then %if even then
            input := input div 2 %Calculation
            put input,", " ..                         
        else
            input := input * 3 + 1 %Calculation
            put input,", " ..                            
        end if 
    end loop 
    put "" 
    put "Do you wish to restart the program?(yes or no)"
    get cont
    if cont ~= "yes" and cont ~= "no"then 
    put "Invalid input, please enter yes or no in lowercase."
    get cont
    end if
    exit when cont = "no"
end loop

-----------------------------------
deworld
Fri Nov 06, 2015 9:38 pm

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
also 2 little questions for the continue at the end is there a way for it to be uppercase and lowercase without having to put another line of code?

and for a put statement how can you list them like a graph without putting 10 different put statements? friend recommends a for loop ... is this the optimal/regular way to do it or is it just for my unique situation?

-----------------------------------
Zren
Sat Nov 07, 2015 10:50 am

RE:I cant seem to use a if statement to check if its even or not to then apply the collatzconjecture on it
-----------------------------------
Make sure both strings are either completely lowercase or uppercase before comparing them.
