/* * Simple program to find prime factorization of large numbers. * * I did this to see how Go compares to Python on this. * It's not as bad as C, * but it still feels like programming in assembly language, * compared to Python. Among other things, * there's no operator overloading * and no way to apply existing algorithms (e.g. square root) * to big.Ints. */ package main
import ( "os" "fmt" "math/big" ) func rough_sqrt(z *big.Int) *big.Int { n := z.BitLen() half_n := n / 2 if n % 2 != 0 { half_n++ } result := big.NewInt(0) result.Exp(big.NewInt(2), big.NewInt(int64(half_n)), nil) return result } func main() { number := os.Args[1] fmt.Printf("%s:", number) z := big.NewInt(0) one := big.NewInt(1) quo, rem := big.NewInt(0), big.NewInt(0) z.SetString(number, 10) max_factor := rough_sqrt(z) for divisor := big.NewInt(2); divisor.Cmp(max_factor) < 0; divisor.Add(divisor, one) { for { // Is there a way to do this division inside the for-condition? quo.QuoRem(z, divisor, rem) if 0 != rem.Sign() { break } fmt.Printf(" %s", divisor) z.Set(quo) max_factor = rough_sqrt(z) } } if z.Cmp(big.NewInt(1)) != 0 { fmt.Printf(" %s", z) } fmt.Printf("\n") } -- To unsubscribe: http://lists.canonical.org/mailman/listinfo/kragen-hacks