I have this function:
def func()
puts "Give a value for x \n>"
x = gets.chomp
puts "Give a value for y \n>"
y = gets.chomp
z = x + y
puts z
end
If a user inputs 5 for x and 5 for y, I want z to make 5 + 5 and print 10, but this will print 55 instead.
The values you've read are stored as strings, and with strings, the + operator performs concatenation. You need to convert both inputs to integers if you want to perform integer arithmetic:
z = x.to_i + y.to_i
Another way to get there:
x = '5'
y = '5'
[x, y].map(&:to_i).inject(:+)
=> 10
z = x.to_i + y.to_i
Just convert input strings to integer or float (to_f)
Related
lets say i have this code
x = 5
while True:
x = 10
break
is there a way I can keep the original value of x and the new assigned value
This pattern might be what you're looking for
x = 5
y = 0
while True:
# y stores old value of x
y = x
x = 10
# example where x actually changes
while True:
y = x
x += 1
print(y, x)
I have a variable x which is set to 10. And I want to write a while loop that increments that. I know you can easily just use a for-loop for this, but easy isn't fun. The code I have is:
def add(a)
g = a + 1
puts g
end
def loop(d)
x = 0
while x <= 4
x += 1
add(d)
end
end
loop(9)
When ran I get 9, four times. How can I get this code to have an output of 9, 10, 11, 12?
Your problem is that you say add(d) and d is the parameter of your loop, loop(d). Ruby does blindly what you tell him : loop(9), so here d=9 and remains equal to 9. You need to increment the value of d. To do that add will now return the incremented value, and we assign the returned value to d (in loop).
To solve your problem you will want to do something like :
def add(a)
g = a + 1
puts g
g
end
def loop(d)
x = 0
while x <= 4
x += 1
d = add(d)
end
end
loop(9)
BUT and that's a huge but, your code is not the ruby way at all.
If I were to do it I would do it like this:
def loop(start_number, repeat_number, increment)
repeat_number.times do
start_number += increment
p start_number
end
end
loop(9, 4, 1)
If you rly wanna to use while and your expected outpit is Array numbers 9 up to 12.
Simply define the array variable before while loop and return it like this:
def loop(number)
x, a = 0, []
while x <= 3
a << number + x
x += 1
end
a
end
loop(9)
# => [9, 10, 11, 12]
BETTER WAY
But better way is use some ruby functions like times and map
in this case we use times and map
def loop(d)
4.times.map{|i| d + i}
end
p loop(9)
# => [9, 10, 11, 12]
maybe this will helps.
I'm trying to do this operation with Ruby:
1.2679769534809603e-175
*
792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313
I know the answer is 1.005025 (truncated to 6 decimal places), but I keep getting the above equation returning infinity. Why is that? I'm not going smaller than the first number, nor larger than the second number.
So two questions: Why is it returning infinity? And how could I get the correct answer to return?
If you want to minimize precision issues, go Rational.
irb(main):001:0> a = 1.2679769534809603e-175.to_r
=> (4519585589664361/35644067325173400145634153169533525975728347712879374457649941546088087243817792082077443838416964060770643043543706307114755505635745609361348916560329798345718708393439569922522454626926592)
irb(main):002:0> b = 792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313
=> 792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313
irb(main):003:0> a * b
=> (3582318778758723293544808766608477208802528713781684733988516016569614012687037578637365969280014511731955915723620354805192948677648150726285518854921630223111683624006805801219885647290056974705691181872855057825408797944671825308998484595184690885834492619019604611321846034964892047367198046135813425296123973237329110031154221763204044754826429491855167243281047603348342563725684284993/35644067325173400145634153169533525975728347712879374457649941546088087243817792082077443838416964060770643043543706307114755505635745609361348916560329798345718708393439569922522454626926592)
irb(main):004:0> (a * b).to_f
=> 1.005025253172702e+200
After some playing around I believe your problem comes from autopromotion of the second number to perform float arithmetic.
puts "Original math"
z = 1.2679769534809603e-175 * 792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313
puts z
Infinity
puts
puts "Save variables individually and inspect"
x = 1.2679769534809603e-175
y = 792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313
puts x
1.2679769534809603e-175
puts y
792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313
z = x * y
puts z
Infinity
puts "What? none of the numbers was Infinity??"
puts "Or was it?? auto promoted y"
y = 792621072814943158937574954417696054502273470568077747007887743862285047941581535541498718312275086275679893343076013862361579680670972527976009279036348551929550827607601145450876014530359530008733947699274904382825445634899233107885545828612637824213482759975963581961375904743254634250508637523339809985946128242523687347261107994804323593105039052556442336528920420940313.0
puts y
Infinity
In most languages anything * Infinity = +- infinity
I am trying to multiple a variable (input from the user) and 4. For some reason, this simple task can not be completed by me.
Here is the code:
print "Enter an Integer between 1 and 12: "
x = gets
puts x * 4
Instead of multiplying x and 4, it will print x a total of four times.
That's because x is a string, and the * method on strings is repetition. You need to convert it to a number using the #to_i method first.
x = gets.to_i
puts x * 4
Should do what you want.
I'll try to be concise this time around! I'm still working Project Euler, this time back to #2. My real issue here is I'm terrible with Ruby. When I run the following code
x = 1
y = 2
sum = 2
while x >= 4_000_000 do |x|
sum += y if y % 2 == 0
z = x + y
x = x ^ y # xor magic
y = x ^ y # xor magic
x = x ^ y # xor magic
y = z
end
p sum
My interpreter kicks out the following output:
/Users/Andy/Documents/Programming/Ruby/ProjectEuler/P2.rb:4: syntax error, unexpected '|'
while x >= 4_000_000 do |x|
^
I'm reading why's (Poignant) Guide to Ruby, and I'm pretty sure I have the pipe syntax correct for the Do. Could someone point out what I'm doing wrong here? I've tried messing around in a lot of different ways and am coming up short handed
while (x >= 4_000_000)
foo
end
You don't even have to pass in x, because it's accessible in the scope of the enclosing block.
while does not take a block. Remove the do |x| part.
while is not a method that takes a block, it is a ruby looping statement. It considers the part between the while and do (or newline) to be the logical test and the part between the do (or newline) and end keyword to be the loop body.
while x < 10 do x += 1; puts x; end
while x < 10
x += 1
puts x
end
Contrast this with something like the Array's each method which takes in a block. Here the each method calls your block for each element of the array (passed into the block as x)
[1,2,3].each do |x|
puts x
end
You accidentally combined the two, asking the while loop to call your code block with the loop counter to be passed in as x. That is not how while works... hence the parsing exception.
What an interesting question! It inspired me to take a shot at the problem, too. Here's my solution.
First, some preparatory work:
class Enumerator
def lazy_select
Enumerator.new do |y|
each do |el|
y.yield(el) if yield el
end
end
end
alias_method :lazy_find_all, :lazy_select
end
module Enumerable
def sum
reduce(:+)
end
end
module Math
ROOT5 = Math.sqrt(5)
PHI = 0.5 + ROOT5/2
def self.fibonacci(n)
Integer(0.5 + PHI**n/ROOT5)
end
end
class Integer
def fibonacci
Math.fibonacci(self)
end
end
Now an Enumerator which generates an infinite sequence of Fibonacci Numbers:
fibs = Enumerator.new do |y|
n = -1
loop do
y.yield (n += 1).fibonacci
end
end
And the nice thing is that we can now directly express the original problem statement in code:
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
puts fibs.lazy_find_all(&:even?).take_while {|n| n <= 4_000_000 }.sum
I think that this is a much more Rubyish way to solve the problem. You write in your question that you are terrible with Ruby. But that's not actually the problem. The real problem is that you are good with C! In other words, the real problem is that you simply aren't writing Ruby, you are writing C with Ruby syntax.
Two good examples are:
y % 2 == 0
and
x = x ^ y
y = x ^ y
x = x ^ y
The Ruby way to write these would be
y.even?
and
x, y = y, x