了解这个 Ruby 程序来查找素数 [英] Understanding this Ruby Program to find prime numbers

查看:36
本文介绍了了解这个 Ruby 程序来查找素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ruby 的新手,我一直在阅读这个查找素数的程序.

I am new to Ruby, and I have been reading over this program that finds us prime numbers.

这个例子来自教科书,在讨论了循环(while 和 until)之后,它展示了这个例子.

This example came in a textbook, after talking about loops (while & until) it showed this example.

我觉得这很令人困惑.prime_flag 的目的是什么?为什么J设为2?

I find it pretty confusing. What is the purpose of the prime_flag? Why is J set as 2?

(j <= i/2) -- 这个我不明白.

(j <= i / 2) -- This one I don't understand.

j = j + 1 -- 这是什么目的.

j = j + 1 -- what is the purpose of this.

我对这么长的问题深表歉意,但非常感谢您的帮助.谢谢,麻烦您了.

My apologies for the long question, but any help is greatly appreciated. Please and thank you.

# Initialize our counter
i = 1

# i: [0, 100]
while (i <= 100)
  # Initialize prime flag 
  prime_flag = true
  j = 2
  # Test divisibility of i from [0, i/2]
  while (j <= i / 2)
    # puts " i ==> " to i.to_s + " j ==> " + j.to_s 
    if (i % j == 0)
      prime_flag = false
      # break
    end 
    j = j + 1
  end
  # We found a prime!
  if prime_flag
    puts "Prime ==> " + i.to_s"
  end 
  # Increment the counter
  i += 1
end

推荐答案

while 循环从 2 到 i 的一半计数并测试它是否为质数.

The while loop is counting from 2 to half of i and testing to see if it is prime.

为什么J设为2?

质数是除一和它本身外没有其他因数的数.如果 j 从 1 开始,当然 1 是数字的一个因数.如果我们包含 1,那么代码会认为没有一个数字是质数,因为它会将 1 视为一个因子.

A prime number is a number that has no factors aside from one and itself. If j started at 1, of course 1 is a factor of the number. If we included 1, then the code would think none of the numbers are prime because it would consider 1 a factor.

(j <= i/2)

(j <= i / 2)

while 循环最多检查数字的一半.从技术上讲,您只需要检查数字的平方根即可.

The while loop checks up to half of the number. Technically, you only need to check up to the square root of the number.

j = j + 1 -- 这样做的目的是什么.

j = j + 1 -- what is the purpose of this.

我们需要增加 j 以移动到下一个数字.该程序本质上是在执行以下操作:

We need to increment j to move on the the next number. The program is essentially doing something like:

  1. 从 2 处的 j 开始.
  2. i 是否可以被 j 整除?
  1. Start with j at 2.
  2. Is i divisible by j?
  1. 是吗?清除主要标志.

  • j 设置为下一个数字,3.
  • i 是否可以被 j 整除?

  • Set j to the next number, 3.
  • Is i divisible by j?

    1. 是吗?清除主要标志.

  • 最多重复一半i
  • 在您发布的示例中,break 被注释掉了,我不知道为什么.break 将是一件好事.这基本上是说,好吧,我们为 i 找到了一个因子,我们不需要继续循环寻找更多的因子.

    In the example you posted, break is commented out, and I'm not sure why. The break would be a good thing to do. That basically says, "OK, we found a factor for i, we don't need to keep looping looking for more factors.

    prime_flag 的目的是什么?

    What is the purpose of the prime_flag?

    prime_flag 用于跟踪是否找到了 i 的任何因素.变量开始时为真,所以是",假设数字是素数.一旦我们找到一个因子,它就会将其设置为 false,表明 i 不是质数.

    The prime_flag is being used to track if it has found any factors for i. The variable starts off as true, so "Yes", assume the number is prime. As soon as we find a factor, it sets it to false, indicating that i is not prime.

    这篇关于了解这个 Ruby 程序来查找素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆