func process(p Problems.Problem) { daftlog.Printf("Problem %d: %s", p.GetID(), p.GetTitle()) p.CalculateAnswer() daftlog.Printf("Answer %d: %s", p.GetID(), p.GetAnswer()) if p.IsAnswerVerified() { daftlog.Printf("Project Euler has verified answer %d as correct.", p.GetID()) } }
func (p3 *problem3) CalculateAnswer() { const BaseNumber = 600851475143 var largestPossiblePrimeFactor = uint(math.Sqrt(BaseNumber)) // largest factor of a number is its square root. // A prime is never even, find the largest odd to start on. if math.Mod(float64(largestPossiblePrimeFactor), 2) == 0 { largestPossiblePrimeFactor -= 1 } daftlog.Printf("Largest possble odd factor of %d = %d", BaseNumber, largestPossiblePrimeFactor) var primeNotFound = true for primeNotFound { if math.Mod(BaseNumber, float64(largestPossiblePrimeFactor)) == 0 { // found an odd factor. var resultOfPrimeTest string if isOddPrime(float64(largestPossiblePrimeFactor)) { // is this odd factor a prime? resultOfPrimeTest = fmt.Sprintf("and %d is a prime!", largestPossiblePrimeFactor) primeNotFound = false // signal that that we're done searching. } else { resultOfPrimeTest = fmt.Sprintf("but %d is not a prime!", largestPossiblePrimeFactor) } daftlog.Printf("Found odd factor at %d, %s", largestPossiblePrimeFactor, resultOfPrimeTest) } if primeNotFound { largestPossiblePrimeFactor -= 2 // skip to next smaller odd number as possible factor. } } p3.answer = fmt.Sprintf("%d", largestPossiblePrimeFactor) p3.answerVerified = true }
func main() { var args = new(cmdline.Arguments) args.Process() daftlog.SetFormat(daftlog.FormatTimeOnly) daftlog.Print(cmdline.GetVersionString()) daftlog.Print("Project Euler via Go, (c) 2015, Lindsay Bradford.") daftlog.Printf( "Problems Implemented: (%d/%d)", config.LastImplementedEulerProblemID, config.MaxEulerProblemID, ) daftlog.Print("") process( Problems.BuildProblem(args.Problem), ) }