Count the numbers in a loop

Hello everyone, I have this code. You type in a number and then it
identifies odd and even numbers. When it identifies an odd number, it
will multiply it by three plus one. When It identifies an even number,
it will divide it by 2. This will go infinitely until it reaches number
1, then the loop will stop.

Does anyone know how to make it count all the numbers that appear on the
screen (until the loop stops at number 1)? I have tried everything in
the last 2 days… but so far not being successful.

Thank you

This is the code:

def f n # this function calculates next number in your sequence
if n % 2 == 0 # n is even
return n / 2
else
return n * 3+1
end
end

num = gets.to_i # read in a number
while num > 1 # infinitely…
num = f(num) # find the next number
puts num # print it
sleep 1 # wait one second
end

On Wed, Feb 8, 2012 at 12:20 PM, Faith T. [email protected] wrote:

Does anyone know how to make it count all the numbers that appear on the
screen (until the loop stops at number 1)? I have tried everything in
the last 2 days… but so far not being successful.

Please show “everything”. This problem is really rather basic so it
can be only a simple error. But if you do not show your code, we will
have to assume that you want us to make your homework for you. I
consider that unethical - plus: you won’t learn anything that way.
Even though it may seem different at times the main purpose of a class
is not passing an exam. It’s all about learning something.

Kind regards

robert

Well there is this then

def f n
… yadda yadda yadda
end

counter = 0

num = gets.to_i
while num > 1
num = f(num)
puts num
counter += 1
end

puts “There were #{counter} numbers displayed”

Also your comments are a waste of time. They convey nothing that the
code does not and in one case is plain wrong. The program does not
loop infinitely, it will terminate when num reaches 1 (and by the way
the syracuse formula - as this is known - will always terminate, it’s
just that no one actually knows when from a given starting number. Say
hello to the halting problem).

Giving your function a completely meaningless name, such as ‘f’, does
not help. If you have to comment a function to say what does you
should just rename your function to something like
find_the_next_number(num) and be done with it.

Ignoring the comment on the sleep statement for a moment, why the
sleep statement at all.

Remember that quite a few teachers google the problems they set to see
if just this sort of thing is happening :slight_smile:

Thanks, Peter for your answer :). I actually had the right code, just
was putting it in the wrong places.

Yes, sorry for the comments… I modified the code and did not change
the comments at the same time. Yes, I’ll definately give a more
reasonable name to the function. This was a quick sketch.

I have another question. The last one, I promise.

Now I need to modify it into a range (of 2 numbers of course). So, for
instance you type in 10 and 30 and it will pick up a number from that
range between 10 and 30 with the highest count and display the highest
count.

For instance, it would display number 112… because that would be the
highest count of a number in that range of 10 and 30.

If anyone knows the answer, I will gladly give him/her a medal :slight_smile:

Faith

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs