Source

snippets / euler / 003-primefactor.go

Full commit
/* project euler problem 003
 * find the largest prime factor of a composite number (600851475143) */

package main

import (
	"fmt"
)

func isprime(num uint64) bool {
	var i uint64

	for i = 2; i < num; i++ {
		if (num % i) == 0 {
			return false
		}
	}
	return true
}

func greatestprimefactor (num uint64) uint64 {
	var i uint64

	if isprime(num) {
		return num;
	}

	for i = 2; i <= num; i++ {
		if (num % i) == 0 && isprime(num/i) {
			return (num/i)
		}
	}

	return 0
}

func main() {
	var num uint64 = 600851475143;
	var result uint64;

	result = greatestprimefactor(num)
	if result > 0 {
		fmt.Printf("%v\n",result)
	} else {
		fmt.Printf("no prime factors found\n")
	}
}