// Finds prime numbers below N
// Returns primes split in blocks for its convenience, they are sorted
Finds prime numbers below N
Returns primes split in blocks for its convenience, they are sorted
// Finds prime numbers below N // Returns primes split in blocks for its convenience they are sorted
func FindPrimes(N Number) (results [][]Number) {
	if N <= 100 {
		var i int
		for PRIMES_100[i] < N { i++ }
		results = [][]Number{PRIMES_100[:i]}
		return
	}
	n := (N.sqrt() + 1)
	results = FindPrimes(n)
	if n < 1000 {
		primes := findPrimesInRange(n, N, results)
		if len(primes) > 0 {
			results = append(results, primes)
		}
	} else {
		numResults := Number(len(results))
		newResults := make(chan []Number)
		numRoutines := 0
		for x := n; x < N; x += n {
			go func(x Number) {
				newResults <- findPrimesInRange(x, (x + n).min(N), results)
			}(x)
			numRoutines++
		}
		for i := 0; i < numRoutines; i++ {
			primes := <-newResults
			if len(primes) > 0 {
				results = append(results, primes)
			}
		}
		sort.Sort(ByFirst(results[numResults:]))
	}
	return
}
func FindPrimes ( N Number ) ( results [ ] [ ] Number ) { if N <= 100 { var i int for PRIMES_100 [ i ] < N { i ++ } results = [ ] [ ] Number { PRIMES_100 [ : i ] } return } n := ( N . sqrt ( ) + 1 ) results = FindPrimes ( n ) if n < 1000 { primes := findPrimesInRange ( n , N , results ) if len ( primes ) > 0 { results = append ( results , primes ) } } else { numResults := Number ( len ( results ) ) newResults := make ( chan [ ] Number ) numRoutines := 0 for x := n ; x < N ; x += n { go func ( x Number ) { newResults <- findPrimesInRange ( x , ( x + n ) . min ( N ) , results ) } ( x ) numRoutines ++ } for i := 0 ; i < numRoutines ; i ++ { primes := <- newResults if len ( primes ) > 0 { results = append ( results , primes ) } } sort . Sort ( ByFirst ( results [ numResults : ] ) ) } return }
Finds prime numbers below N
Returns primes split in blocks for its convenience, they are sorted
Finds prime numbers below N Returns primes split in blocks for its convenience they are sorted
// fmt.Println("findNonPrimes(", N, nonPrimes, ")") // defer func() { // 	fmt.Println("    nonPrimes", nonPrimes) // }() // Primes below 100 are hard coded: // Recursively finds non-primes below or equal to sqrt(N) // Fills serially the buffer of non primes: // Fills on parallel the buffer of non primes: // Sorts only new results: