Ruby determines whether a number is a prime number.

Source: Internet
Author: User

Ruby determines whether a number is a prime number.

Prime number. A natural number greater than 1 is called a union number if it cannot be divided by any other natural number except 1 and itself. According to the basic arithmetic theorem, each integer greater than 1 is either a prime number or a product of a series of prime numbers. If the order of these prime numbers in the product is not considered, the written form is unique.

Copy codeThe Code is as follows:
Def prime? (Num)
Res = [1]
Res <num

If num = 0 | num = 1
Return false
End

2. upto (10) do | x |
# If you have your own, you can skip a loop.
If num = x
Next
End

# Check whether the remainder can be divisible by a number between 2 and 10. The remainder is equivalent to a group.
If num % x = 0
Res <x
End
End

Res. length> 2? False: true
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.