The prime number filter in Go

Source: Internet
Author: User
Tags square root
This is a creation in Article, where the information may have evolved or changed.

~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~

Examination problem

Prime number, also known as Prime number, has an infinite number. A prime number is defined as the number of natural numbers greater than 1, except that 1 and itself no longer have other factors known as prime numbers.

Determine how many primes are in between 101-200 and output all primes.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Analysis

Program Analysis: The method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa is a prime.

In the general domain, the positive integer n, if all integers between 2 and square root n are removed, are not divisible, then n is prime.

A prime number greater than or equal to 2 cannot be divisible by its own and 1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Program

Func isprime (value int) bool {    if value <= 3 {        return value >= 2    }    if value%2 = = 0 | | value%3 = = 0 {        return False    }    For I: = 5; I*i <= value; i + = 6 {        if value%i = = 0 | | value% (i+2) = = 0 {            return false        }    }    return True}


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.