Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm testing the reserved word unless with the following code, which increments the value of x.
x = 1
unless x >= 2
puts "x is less than 2"
else
puts "x is greater than 2"
x = x + 1
end
The output is:
x is less than 2
Does unless support the increment of x?
Your question is not clear. But it is clear that the only place in your code that increments a variable:
x = x + 1
is not executed because the condition unless x >= 2 is always met.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I check if a number is a Dudeney Number, using Ruby? Taking as examples or evidence, these results:
dudeney_number?(1) #=> true
dudeney_number?(125) #=> false
dudeney_number?(512) #=> true
dudeney_number?(1728) #=> false
dudeney_number?(5832) #=> true
Well, that's easy; there are only 6!
def dudeney_number?(x)
return [1, 512, 4913, 5832, 17576, 19683].include?(x)
end
See also: http://blog.hostilefork.com/six-dudeney-numbers-proof/
From https://en.wikipedia.org/wiki/Dudeney_number:
A Dudeney number is a positive integer that is a perfect cube such that the sum of its decimal digits is equal to the cube root of the number.
Here's an implementation by that definition:
def dudeney_number?(n)
digit_sum = n.to_s.chars.map(&:to_i).inject(:+)
n == digit_sum ** 3
end
1.upto(20000).select { |n| dudeney_number? n }
#=> [1, 512, 4913, 5832, 17576, 19683]
The method doesn't check if n is a positive integer.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
a = 1; b = 2
fibonacci = []
while fibonacci.length < 100
fibonacci.push(a)
fibonacci.push(b)
a = a + b; b = a + b
end
push fibonacci
The error message is "undefined method `push' for main:Obj"
You're trying to #push the array itself on the last line! :)
That's what it's complaining about. The push method is being invoked on the 'main' object, and push is not a Kernel method.
I'm guessing you mean puts. Otherwise it looks okay, if somewhat non-idiomatic. Naturally you can find lots of Ruby solutions for this problem on the site that might read a bit more clearly (see here for a recursive one.)
As others have said before the last line should be 'puts'
Also your numbers are wrong.
a = 1; b = 1
fibonacci = []
while fibonacci.length < 100
fibonacci << a
fibonacci << b
a += b
b += a
end
puts fibonacci
But also the fib starts at 1 and the the second element is also 1.
This make you sequence off, if you start at 1, 2
Fib = 1, 1, 2, 3, 5, 8, ...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am answering the following question:
Q3: Write a method, is_prime?, that takes a number num and returns
true if it is prime and false otherwise.
You may wish to use the modulo operation: 5 % 2 returns the remainder
when dividing 5 by 2: 1. If num is divisible by i, then num % i == 0.
(You would not be expected to already know about modulo for the
challenge)
but I get no answer back. My final "puts" doesn't seem to be working. Here is my code:
# Q3 method is_prime?
def is_prime? (num)
i = 2
while i <= num
is_divisble = ((num%i) == 0)
if is_divisble
return false
end
i+=1
end
# is not divisible
return true
end
puts 'select a number greater than one and see if it\'s prime'
num = gets.chomp.to_i
puts is_prime?(num)
The code prints fine. However, it always prints true. Remedy: change while i <= num
to while i < num .
i <= num
It is your mistake :).
Your code will increment i until i == num so num%i will always be true at the end.
Just replace it with
i < num
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
B = 3
while(B <= 11):
print(B)
B = B+2
I already tried a bunch of stuff, all of it crap, like:
for B <= 11:
which is apparently invalid syntax, and i've tried:
B= 3
if B <= 11:
print(B)
B = B+2
which does absolutely nothing
So, any suggestions?
Looks like you're learning Python. What you are looking for is a range:
for B in range(3,12,2):
print(B)
Note that the parameters here are 3, 12 and 2.
The 3 is the starting point.
The 12 is used instead of 11 for the end of the range, because the range() function in Python excludes the last value of the range. You'll want to keep that in mind when writing Python code.
The 2 is the step value.
You can use something like this (Java):
for(int B = 3; B <= 11; B += 2)
{
System.out.println(B);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Accept an integer n
Based on n, accept n inputs
Ex:
At runtime
n = 2
Then 2 inputs of type string should be accepted
Ex:
At runtime
n = 3
Then 3 inputs of type string should be accepted
array = []
n.times {array << gets.chomp}
n = gets.chomp.to_i
array = []
array[n] = gets.chomp while (n -= 1) >= 0
# array will contain the inputs...
# array.pop will return them in order of input