Last active
October 10, 2015 06:47
-
-
Save luv2code/3649847 to your computer and use it in GitHub Desktop.
Problem with references?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func isPrimeDivisible(c int) bool { | |
prime := 0 | |
for i := 0; i < prime_count; i++ { | |
prime = primes[i] | |
if (prime * prime) > c { | |
return false | |
} | |
if (c % prime) == 0 { | |
fmt.Printf("c mod prime %v, c %v, prime %v \n", (c%prime), c, prime) | |
return true | |
} | |
} | |
return false | |
} | |
var primes = make([]int, 1, 25000) | |
var prime_count = 1 | |
func main() { | |
primes[0] = 2 | |
c := 3 | |
fmt.Printf("Primes %v", primes) | |
for prime_count < 25 && c < 100000{ //25000 { | |
fmt.Printf("isDivisible? %v\n", isPrimeDivisible(c)) | |
if !isPrimeDivisible(c) { | |
primes = append(primes, c) | |
prime_count++ | |
} | |
c++ | |
} | |
fmt.Println(primes[prime_count-1]) | |
} |
Well I looked over it and there are a couple problems:
- 1 is not a prime number. You should set primes[0] = 2
- You start at c = 1, but for the algorithm to work without duplicates you need c > primes[prime_count-1], so start with c = 3
After that it works just fine.
thank you very much
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is supposed to print out the first 25 primes. It doesn't work and I can't figure out why.