# Arguments with Funktion

i want a ruby program, is have 2 Arguments(a,b) ,which output the Prime
zahlen between a and b. hier is my program. but it’s wrong. i don’t know
where is the problem. can anyone help me?

a,b=ARGV
def prime (a,b)
for i in 2…b
f=true
for p in 2…i
if i%p==0
f =!f
break
end
end
print i, “–” if f
end
end

sushu=prime(a,b)
p “the prime between #{a} and #{b} is #{sushu}.”

roboter Y. wrote in post #1056317:

i want a ruby program, is have 2 Arguments(a,b) ,which output the Prime
zahlen between a and b. hier is my program. but it’s wrong. i don’t know
where is the problem. can anyone help me?

Replace

``````        f =!f
``````

with

f = false

p “the prime between #{a} and #{b} is #{sushu}.”

Also, the last line doesn’t do anything meaningful, because you do not
return the prime numbers, you print them.

Michal M. wrote in post #1056320:

roboter Y. wrote in post #1056317:

i want a ruby program, is have 2 Arguments(a,b) ,which output the Prime
zahlen between a and b. hier is my program. but it’s wrong. i don’t know
where is the problem. can anyone help me?

Replace

``````        f =!f
``````

with

f = false

p “the prime between #{a} and #{b} is #{sushu}.”

Also, the last line doesn’t do anything meaningful, because you do not
return the prime numbers, you print them.

i have try, but it’s still wrong,
wenn ich :
ruby prime.rb 2 50
prime.rb:3:in `prime': bad value for range (ArgumentError) from prime.rb:15:in`

Michal M. wrote in post #1056320:

Replace

``````        f =!f
``````

with

f = false

This doesn’t make a difference. Both statements set f to false (though
yours is of course clearer and eventually safer).

@ roboter Y.:

What makes you think your program is wrong? It does work correctly
(assuming you want to print out the numbers).

However, your programming style isn’t very “rubyish”. It looks more like
a Java program or so.

In Ruby, it’s common to use iterators instead of loops. The advantage of
iterators is that all variables created inside the block are local
(whereas variables created in loops still exist after the loop has
finished). There are also useful iterators like Enumerable#all? and
Enumerable#any?, which you can use to avoid low level stuff:

def print_primes_between(a, b)
2.upto b do |i|
print i, ‘–’ unless (2…i).any? {|j| i % j == 0}
end
end

// edit:

roboter Y. wrote in post #1056323:

ruby prime.rb 2 50
prime.rb:3:in `prime': bad value for range (ArgumentError) from prime.rb:15:in`

You have to convert the strings coming from ARGV to integers:

a,b=ARGV
prime(a.to_i, b.to_i)

On Fri, Apr 13, 2012 at 8:09 AM, Peter H. <
[email protected]> wrote:

Actually there are better ways to check for prime numbers between a
and b (hint: 2 is the only even prime, why are you even looking at
any others?)

In the same way you can rule out all all multiples of 2, you can rule
out
all multiples of 3 and 5 and 7 and so on. This means you only need to
see
if your number is divisible by any primes (if it is divisible by 4 or 6
or
8, then it is also divisible by 2).

So we can write it like this:

def prime(upper_limit)
return [] if upper_limit < 2
(3…upper_limit).each_with_object [2] do |potential_prime, primes|
next if primes.any? { |prime| potential_prime % prime == 0 }
primes << potential_prime
end
end

prime 1 # => []
prime 2 # => [2]
prime 3 # => [2, 3]
prime 4 # => [2, 3]
prime 5 # => [2, 3, 5]
prime 6 # => [2, 3, 5]
prime 7 # => [2, 3, 5, 7]
prime 8 # => [2, 3, 5, 7]
prime 9 # => [2, 3, 5, 7]
prime 10 # => [2, 3, 5, 7]
prime 11 # => [2, 3, 5, 7, 11]
prime 12 # => [2, 3, 5, 7, 11]
prime 13 # => [2, 3, 5, 7, 11, 13]
prime 14 # => [2, 3, 5, 7, 11, 13]
prime 15 # => [2, 3, 5, 7, 11, 13]
prime 16 # => [2, 3, 5, 7, 11, 13]
prime 17 # => [2, 3, 5, 7, 11, 13, 17]
prime 18 # => [2, 3, 5, 7, 11, 13, 17]
prime 19 # => [2, 3, 5, 7, 11, 13, 17, 19]
prime 20 # => [2, 3, 5, 7, 11, 13, 17, 19]

If we wanted to get elaborate, we could introduce a cutoff at the square
root:

def prime(upper_limit)
return [] if upper_limit < 2
(3…upper_limit).each_with_object [2] do |potential_prime, primes|
cutoff = Math.sqrt potential_prime
next if primes.take_while { |prime| prime <= cutoff }.any? { |prime|
potential_prime % prime == 0 }
primes << potential_prime
end
end

If you needed this to be super fast, there would be better ways to write
this (as in faster, but harder to understand and read), but this shows
the
basic idea.

Firstly argument a is not needed as you do not use it.

Secondly there are several prime numbers between 2 and 50 so you
should be returning a list.

Here is a quick hack of your code

b = 50 # ARGV

def prime(b)
prime_numbers = Array.new

for i in 2…b
f=true
for p in 2…i
if i%p==0
f = false
break
end
end
prime_numbers << i if f
end

prime_numbers
end

sushu=prime(b)
p “the prime between 2 and #{b} are #{sushu.inspect}.”

How lets make it a bit more Rubyish and readable…

def prime(upper_limit)
prime_numbers = Array.new

(2…upper_limit).each do |number_to_check|
is_a_prime_number = true
(2…number_to_check).each do |factor|
if number_to_check % factor == 0
is_a_prime_number = false
break
end
end
prime_numbers << number_to_check if is_a_prime_number
end

prime_numbers
end

Now lets mix in Jan E. suggested code

def prime(upper_limit)
prime_numbers = Array.new

2.upto upper_limit do |number_to_check|
prime_numbers << number_to_check unless (2…number_to_check).any?
{|factor| number_to_check % factor == 0}
end

prime_numbers
end

Actually there are better ways to check for prime numbers between a
and b (hint: 2 is the only even prime, why are you even looking at
any others?)