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 7 years ago.
Improve this question
I am trying to write an algorithm to solve the math problem Σ n = 1 to 49 of n(n+1). I keep getting an error "undefined method 'n' for main object"
def solver(n)
sum = 0
while n < 49
temp = n(n+1)
n+=1
sum = sum + temp
end
return sum
end
puts solver(1)
Instead of:
temp = n(n+1)
put:
temp = n*(n+1)
The reason you're getting undefined method 'n' for main object is because your code thinks that n(n+1) is calling n() method on the main object. In ruby main refers to the top level object which is an instance of Object
In any case, the solution to your issue is actually multiplying n * (n + 1) rather than calling a method n with argument n + 1
This is because of missing *. Try this solution:
def solver(n)
sum = 0
while n < 49
temp = n*(n+1)
n+=1
sum = sum + temp
end
return sum
end
puts solver(1)
Related
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 3 years ago.
Improve this question
I just start learning ruby with an online course and in my very first exercise, I can't complete the challenge. I have to create functions to sum, subtract, multiply and divide that meet the specs conditions.
For the division, I need to check if it will divide per 0 or not, give the result or a warning. My code is
def divisao(primeiro_numero, segundo_numero)
if segundo_numero > 0
primeiro_numero / segundo_numero
else
puts 'Opa! Zero como divisor'
end
But when I run the specs, I get the following warning
0 examples, 0 failures, 1 error occurred outside of examples
I have changed all the functions, but this one doesn't seem to work.
I added the complete functions and specs file here:https://gist.github.com/isafloriano/86c170400b2f5fc63dc5e8edd8913525
Can anyone give me a clue why this doesn't work?
You are just missing the end of the if-else block:
def divisao(primeiro_numero, segundo_numero)
if segundo_numero > 0
primeiro_numero / segundo_numero
else
puts 'Opa! Zero como divisor'
end # <= this one was missing
end
Some suggestion:
Use English method and variable names. That makes it easier for others to understand your code and to help you.
Try to follow common Ruby conventions right from the start. Ruby code is indented with two spaces.
Here's a compact way to write your four methods.
def add(n1, n2)
f(n1, n2, :+)
end
def subtract(n1, n2)
f(n1, n2, :-)
end
def multiply(n1, n2)
f(n1, n2, :*)
end
def divide(n1, n2)
if n2.zero?
puts "Zero-divide error"
return nil
end
f(n1, n2, :/)
end
def f(n1, n2, op)
n1.public_send(op, n2)
end
add(3, 7) #=> 10
add(5, 7.2) #=> 12.2
subtract(4, 8) #=> -4
subtract(6.1, 3) #=> 3.0999999999999996
multiply(2, 6) #=> 12
multiply(1.0, 6) #=> 6.0
divide(4, 0) #=> nil
Zero-divide error
divide(4, 0.0) #=> nil
Zero-divide error
divide(4, 2) #=> 2
divide(5, 2) #=> 2
divide(5, 2.0) #=> 2.5
See Object#public_send.
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 8 years ago.
Improve this question
Who can tell me how to solve the error as title?
How to use a nested if-else sentence?
Thank U.
b = Array.new(n,0)
c = Array.new(n) {Array.new(n,0)}
n.times do |i|
a[i], b[i] = gets.split(" ").map(&:to_i)
end
n.times do |i|
w.times do |j|
i=i+1
j=j+1
if a[i-1] > j || i > k then
c[i][j] = c[i-1][j]
else
if c[i-1][j] < c[i-1][j-a[i-1]]+b[i-1] then
c[i][j] = c[i-1][j-a[i-1]]+b[i-1]
k--
else
c[i][j] = c[i-1][j]
end
end
end
end
There is no such thing as the increment/decrement operator in Ruby.
Replace k-- with k -= 1.
You could also use compound operators for these:
i += 1 # previously i=i+1
j += 1 # previously j=j+1
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
Below is a prime number calculator I've been working on. Currently it can calculate 100,000 prime numbers in about 22 seconds on my computer. Any thoughts on how to make the below program quicker programatically or mathematically?
include Math
print "How many prime numbers do you want?:"
x = gets.chomp.to_i #want x primes
c = 0 #want c primes
t = 3 #test number
d = 1 #divisor
n = 0 #current number of divisors
puts "2"
c+=1
while c < x
while n <= 1 && d <= Math.sqrt(t)
if(t % d == 0)
n+=1
d+=2
else
d+=2
end
end
if(n == 1)
c+=1
puts "#{t}"
end
t+=2
d = 1
n = 0
end
A fast way to find all primes up to N (or the first n primes) is to use what's called a sieve. The idea is you start with a list of integers 2,... M and associate with each either a boolean value of True, or integer 1. Then starting with 2, make every multiple of 2 (bigger than 2) false (or zero). None of these are prime because they are all divisible by 2. Then, in your list, find the next smallest value that still has True (3 in this case). Then, set all multiples of 3 (bigger than 3) to False. Then repeat. Find the next smallest number in the list that is still marked True. (4 was marked false because it was a multple of 2), 5 is the next choice. Repeat, repeat, repeat... until the smallest one you find is bigger than sqrt(M). Then every value in your list with True is a prime. This will be asymptotically much much faster than what your code does (for every value try to find a divisor).
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