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
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 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.
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 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 9 years ago.
Improve this question
Two lists of numbers, one with the input pos numbers, and one
with those that are neg numbers (ignore the zero-valued numbers)
Method: You must first build two arrays with the required output numbers, before you
display any of them
the below code is not ruby. how can i convert it to ruby?
# Loop to read input and build the two new arrays
while ($next = <>) {
if ($next > 0) {
push #pos_list, $next;
}
else {
if ($next < 0) {
push #neg_list, $next;
}
}
}
# Output the results
print "The array of positive numbers: \n #pos_list \n";
print "\nThe array of negative numbers: \n #neg_list \n";
numbers = [4,-2,7,3,0,-8]
pos_list = numbers.select {|x| x > 0}
neg_list = numbers.select {|x| x < 0}
p pos_list # => [4, 7, 3]
p neg_list # => [-2, -8]
numbers is your array of numbers, which you have built from user input. Array#select returns a new array containing all elements that cause the attached block to evaluate to true. See: http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-select
Assuming numbers to be the array of numbers from the input:
pos = []
neg = []
numbers.each do |n|
pos += n if n > 0
neg += n if n < 0
end
After the above code executes you can retrieve positive numbers from pos and negative ones from neg.
How you populate numbers can vary. A possible solution is too loop and keep asking for a number:
numbers = []
begin
current = gets.chomp.to_i
numbers << current if current > 0
end until current == 0
assuming you don't want 0 to be part of number. Otherwise you'll have to check for a given input to stop the loop. Alternatively you can have a fixed size of numbers.
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
In an effort to speed up my program, I'm trying to combine these two methods, but am having the hardest time doing it. Here are the methods:
def prime?(number, array)
array.each do |x|
if number % x == 0
return false
end
end
true
end
def sum_prime_2(number)
i = 0
prime = 1
output = [2]
while prime < number
if prime?(prime, output)
i += 1
output << prime if prime != 1
end
prime += 2
end
output.inject(:+)
end
And here is what I've come up with, but it's not quite working. I'd love any help.
def sum_prime(number)
i = 0
prime = 1
output = [2]
while prime < number
if output.each { |x| prime % x == 0 } == true # prime? method
output << prime if prime != 1
i += 1
end
prime += 2
end
output.inject(:+)
end
Here's a simplification of your current approach:
def sum_primes(limit)
primes = [2]
n = 3
while n < limit
primes << n if primes.all? { |p| n % p != 0 }
n += 2
end
primes.inject(:+)
end
But you can do better. For example, there's no need to check for divisibility by all prior primes -- only up to the square of n. Even better would be a sieve approach, particularly an incremental one.
I would actually use this code, rather than combining the two functions:
def prime?(number, array)
array.each do |x|
if number % x == 0
return false
end
return true if x * x > number
end
true
end
Doing a quick benchmark for sum_prime_2(100000) versus FMc's answer versus mine, the original code takes about 5.0 seconds, FMc's takes 6.5 seconds, but my version just 0.1 seconds.
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