为什么犯规我红宝石编码查找素数的工作?素数、红宝石、工作

2023-09-11 00:13:05 作者:南风向北

我想知道为什么我的code不起作用。我是新来的code的世界,所以如果有人能打破这个问题下来,我怎么会是最好的解决呢谢谢!

I am wondering why my code does not work. I am new to the code world so if anyone can break this problem down for me and how would be best to solve it thanks!

我想创建一个程序,将表明质数从我指定号码列表。

I am trying to create a program which will indicate the prime numbers from a list of numbers that I specify.

请告诉我,为什么这两个codeS不工作!我很困惑,什么第二code是试图做,因为我发现它是别人的解决我的问题。我是新来的编码,但我喜欢它,所以我承担!

Please tell my why these two codes do not work! I am confused as to what the second code is trying to do as I found it as someone else's solution to my problem. I am new to coding but I love it so bear with me!

下面是我简单的code:

Here is my simple code:

def is_prime?(*nums)

    i = 2
    nums.each do |num|
        while i < num
            if num % i == 0
                puts "#{num} is not a prime"
            else
                puts "#{num} is a prime"
            end
            i += 1
        end
    end
end

....为什么这个不工作?我怎样才能得到它的工作?它不断给我处理会在未来数我插入的时候,我把一个奇怪的答案,因为它被卡住我的第一个数字和犯规:

....Why does this not work? How can I get it to work? It keeps giving me a weird answer in that it gets stuck on my first number and doesnt seem to process the next numbers I insert when I put:

puts is_prime?(21, 23, 17)

这是第二个code说我不是能够正确要么运行。有人可以打破这里究竟是怎么回事呢?我怎样才能得到它的工作?

Here is a second code that I wasnt able to run correctly either. Can someone break down what is going on here? How can I get it to work?

def is_prime?(*nums)
    nums.each_with_object({}) do |num, hsh|
        hsh[num] = num > 1 && 2.upto(num - 1).none? {|i| num % i == 0}
    end
end

puts is_prime?(27, 13, 42)

不管怎样,我知道这个问题是有点混乱,但如果有人在乎输入他们的2分钱我就会AP preciate吧!哦,最后我如何使用code对问题栏这是否正确?我是很新,很迷茫没有导师!

Anyways, I know this question is kind of confusing but if anyone cares to input their 2 cents I would appreciate it! Oh and lastly how do I post code on question board this correctly? I am so new and so confused without a mentor!

推荐答案

您有几个问题。一个是较早鉴定:该语句的位置 I = 2 。这是你的code与固定的。

You have a few problems. One was identified earlier: the location of the statement i = 2. Here is your code with that fixed.

def is_prime?(*nums)
    nums.each do |num|
    i = 2
      while i < num
        if num % i == 0
          puts "#{num} is not a prime"
        else
          puts "#{num} is a prime"
        end
        i += 1
      end
   end
end

NUM%我== 0 你确定的数字是不是素数,并打印相关消息,但你继续检查,如果它看到整除比 NUM 减去所有较大的数字。每次 NUM%我== 0 打印出来,这不是素数。问题的关键是,你不需要继续检查,一旦你确定一个数是不是素数。

When num % i == 0 you've determined the number is not prime, and print a message to that effect, but then you continue checking to see if it is divisible all larger numbers less than num. Each time num % i == 0 you print out that it's not prime. The point is that you don't need to keep checking once you determine a number is not prime.

另外一个问题是,每当 NUM%I!= 0 您打印的数量是素数。这是premature,但是。你不能得出这样的结论,直到确定 NUM%I!= 0 不到 NUM 所有整数。

Another problem is whenever num % i != 0 you print that the number is prime. That's premature, however. You can't draw that conclusion until you determine that num % i != 0 for all integers less than num.

让我们看看如何解决这些问题。我认为,最简单的方法是写一个单独的方法是确定一个单一的数字是素数。我叫这种方法 is_prime?,并更名为主要方法 is_each_prime?

Let's see how to fix these problems. I think the easiest ways is to write a separate method that determines if a single number is prime. I've called that method is_prime? and renamed the main method is_each_prime?.

def is_each_prime?(*nums)
  nums.each { |num|
    puts "#{num} is #{ is_prime?(num) ? '' : "not " }a prime" }
end

def is_prime?(num)
  !(2...Math.sqrt(num)).any? { |i| num % i == 0 }
end

puts is_each_prime?(21, 23, 17)
  #=> 21 is not a prime
  #   23 is a prime
  #   17 is a prime

创建一个单独的方法的一个优点 is_prime?是,你可以单独进行测试,以确保其工作正常。

One advantage of creating a separate method is_prime? is that you can test it separately, to make sure it is working properly.