Forum: Ruby Help with a loop

388f04090c265db12c3bb7830a1b2c99?d=identicon&s=25 Lukas Lohnicky (dronesohard)
on 2013-10-27 02:35
Hey I'm a noob at ruby so far but learning fast

Basically how could I create a loop

Instead of doing this :

op1 = ((num1%100)/10)
result1 = op1*num2
puts "#{result1}"

op2 = ((num1%1000)/100)
result2 = op2*num2
puts "#{result2}"

op3 = ((num1%10000)/1000)
result3 = op3*num2
puts "#{result3}"

op4 = ((num1%1000000)/100000)
result4 = op4*num2
puts "#{result4}"

op5 = ((num1%10000000)/1000000)
result5 = op5*num2
puts "#{result5}"

op6 = ((num1%100000000)/10000000)
result6 = op6*num2
puts "#{result6}"

op7 = ((num1%10000000000)/100000000)
result7 = op7*num2
puts "#{result7}"

op8 = ((num1%100000000000)/1000000000)
result8 = op8*num2
puts "#{result8}"

op9 = ((num1%1000000000000)/10000000000)
result9 = op9*num2
puts "#{result9}"

So that every time it puts in the zero like the example, but also
outputs the result. for the number of times the loop will happen i will
be using

nummultil = (num1 * num2).to_s.length
num1l = num1.to_s.length
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2013-10-27 02:41
(Received via mailing list)
>
>

Here is an idea trying not to stray too much from your code.
Modify it the way you want.

(1..9).each do |x|
  p num2 * (num1%(10**(x+1))/(10**x))
end



Harry
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2013-10-27 08:51
(Received via mailing list)
# Fix the 'max=10' to fit what you need as maximun iterations count.

def lukas_loop(num1, num2)
  max = 10 # I didn't understand how you calculated the max
iteractions counting.
  1.upto(max) do |n|
    op = ((num1 % (10**(n+1))) / 10**n )
    result = op*num2
    puts "#{result}"
  end
end


Is it something like this?
388f04090c265db12c3bb7830a1b2c99?d=identicon&s=25 Lukas Lohnicky (dronesohard)
on 2013-10-27 15:31
Abinoam Jr. wrote in post #1125746:
> # Fix the 'max=10' to fit what you need as maximun iterations count.
>
> def lukas_loop(num1, num2)
>   max = 10 # I didn't understand how you calculated the max
> iteractions counting.
>   1.upto(max) do |n|
>     op = ((num1 % (10**(n+1))) / 10**n )
>     result = op*num2
>     puts "#{result}"
>   end
> end
>
>
> Is it something like this?

Ok you guys get the idea, but i need it to output every single time it
does the loop will the example you gave do that ?

Also i will be calculating the amount it loops by the .length of "num1"
so that it cycles through each of the digits and then multiplies each of
the digits by num2

Thanks a lot
-lukas


Edit: I tried your method but it doesn't seem to work, it doesnt output
anything
388f04090c265db12c3bb7830a1b2c99?d=identicon&s=25 Lukas Lohnicky (dronesohard)
on 2013-10-27 15:48
I worked it out I think


print "enter the first number: "
num1 = gets.chomp.to_i

print "enter the second number: "
num2 = gets.chomp.to_i

nummultil = (num1 * num2).to_s.length
num1l = num1.to_s.length



  num1l.times do |n|
    op = ((num1 % (10**(n+1))) / 10**n )
    result = op*num2
    puts "#{result}"
  end

just like this it work

Thanks a lot

-lukas
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.