go求质数

package main

import (
	"fmt"
	"math"
)

func isPrime(n int) bool {
	if n <= 1 {
		return false
	}
	stop := int(math.Sqrt(float64(n)))
	for i := 2; i <= stop; i++ {
		if n % i == 0 {
			return false
		}
	}
	return true
}

func main() {
	for val := 100; val <= 200; val++ {
		if isPrime(val) {
			fmt.Println(val)
		}
	}
}
posted @ 2022-02-26 22:53  ty1539  阅读(48)  评论(0编辑  收藏  举报