The go language asks for prime numbers within 1 to 100

Source: Internet
Author: User

The prime number refers to a number of natural numbers greater than 1 that cannot be divisible by other natural numbers except 1 and the integer itself. In other words, the natural number of only two positive factors (1 and self) is prime (also called prime). A number larger than 1, but not prime, is called composite. 1 and 0 are not prime or composite.

package mainimport (    "fmt"    "math")func main() {    var i,j,n int    var a[101] int    for i = 1;i <= 100; i++ {        a[i] = i    }    a[1] = 0        // math.Sqrt 开平方    for i = 2; i < int(math.Sqrt(100));i++ {        for j = i +1;j <= 100;j++ {            if (a[i] != 0) && (a[j] != 0) {                if a[j]%a[i] == 0 {                    a[j] = 0                }            }        }    }    fmt.Println()    for i, n = 1, 0;i <= 100; i++ {        if a[i] != 0 {            fmt.Print(a[i],"\t")            n++        }        if n == 10 {            fmt.Print()            n = 0        }    }    fmt.Printf("\n")}

The go language asks for prime numbers within 1 to 100

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.