func C050() (interface{}, error) { limit := 1000000 primes := utils.PrimesLowerThan(limit) totalLength := len(primes) asKeys := map[int]bool{} for _, prime := range primes { asKeys[prime] = true } max := 0 maxValue := 0 for start := 0; start < totalLength; start++ { sum := 0 for length := 0; length < totalLength-start; length++ { sum += primes[start+length] if _, found := asKeys[sum]; found { if max < length+1 { // fmt.Println(primes[start:start+length+1], length+1, sum) max = length + 1 maxValue = sum } } if sum >= limit { break } } } return maxValue, nil }
func C010() (interface{}, error) { sum := 0 for _, prime := range utils.PrimesLowerThan(2000000) { sum += prime } return sum, nil }
func C046() (interface{}, error) { upperLimit := 10000 // prepare maps primes := utils.PrimesLowerThan(upperLimit) primesMap := map[int]bool{} twiceSquareMap := []int{} for i := 1; ; i++ { twiceSquare := 2 * i * i if twiceSquare > upperLimit { break } twiceSquareMap = append(twiceSquareMap, twiceSquare) } for _, prime := range primes { primesMap[prime] = true } // bruteforce for i := 3; i < upperLimit; i++ { if i%2 == 0 { // skipping even numbers continue } if primesMap[i] { // skipping prime numbers continue } found := false for _, prime := range primes { if found { break } if prime > i { break } for _, twiceSquare := range twiceSquareMap { sum := prime + twiceSquare // fmt.Printf("%d <-> %d+%d=%d\n", i, prime, twiceSquare, sum) if sum == i { found = true break } if sum > i { break } } } if !found { return i, nil } } return nil, fmt.Errorf("no such odd composide number bellow %d", upperLimit) }
func C041() (interface{}, error) { max := 0 upperLimit := 987654321 primes := utils.PrimesLowerThan(upperLimit) for _, prime := range primes { if utils.IsPandigital(strconv.Itoa(prime)) { if prime > max { max = prime // fmt.Println(prime) } } } return max, nil }
func C037() (interface{}, error) { sum := 0 for _, prime := range utils.PrimesLowerThan(1000000) { if prime < 10 { continue } primeStr := strconv.Itoa(prime) ok := true for i := 1; i < len(primeStr); i++ { if !isStrPrime(primeStr[i:]) || !isStrPrime(primeStr[:i]) { ok = false break } } if !ok { continue } sum += prime } return sum, nil }
func C007() (interface{}, error) { // FIXME: use another method primes := utils.PrimesLowerThan(1000000) return primes[10000], nil }