Arithmetic/Rational: Difference between revisions

→‎{{header|Go}}: library path update, gofmt, optimized, fixed squares, added output
(→‎{{header|Go}}: library path update, gofmt, optimized, fixed squares, added output)
Line 863:
=={{header|Go}}==
Go's <code>big</code> package implements arbitrary-precision integers and rational numbers.
<lang go><package main
 
import (
"fmt"
"bigmath"
"math/big"
)
 
func main() {
var recip big.Rat
max := int64(1<<19)
for candidate max := int64(2); candidate1 << max; candidate++ {19)
sumfor candidate := big.NewRatint64(1,2); candidate) < max; candidate++ {
max2 sum := int64(mathbig.Sqrt(float64NewRat(1, candidate)))
max2 := int64(math.Sqrt(float64(candidate)))
for factor := int64(2); factor <= max2; factor++ {
if candidate %for factor := int64(2); factor <= 0max2; factor++ {
sum.Add(sum, new(big.Rat).Add(big.NewRat(1, factor), big.NewRat(1, if candidate%factor /== factor)))0 {
sum.Add(sum, recip.SetFrac64(1, factor))
}
for factor if f2 := int64(2);candidate / factor; <=f2 max2;!= factor++ {
sum.Add(sum, recip.SetFrac64(1, f2))
}
}
}
if sum.Denom().Int64() == 1 {
perfectstring := ""
candidate,if sum.Num().Int64(), perfectstring)== 1 {
if sum.Num().Int64() == 1 { perfectstring = "perfect!" }
}
fmt.Printf("Sum of recipr. factors of %d = %d exactly %s\n",
candidate, sum.Num().Int64(), perfectstring)
}
}
if sum.Denom().Int64() == 1 {
perfectstring := ""
if sum.Num().Int64() == 1 { perfectstring = "perfect!" }
fmt.Printf("Sum of recipr. factors of %d = %d exactly %s\n",
candidate, sum.Num().Int64(), perfectstring)
}
}
}</lang>
Output:
 
<pre>
It might be implemented like this:
Sum of recipr. factors of 6 = 1 exactly perfect!
 
Sum of recipr. factors of 28 = 1 exactly perfect!
[insert implementation here]
Sum of recipr. factors of 120 = 2 exactly
Sum of recipr. factors of 496 = 1 exactly perfect!
Sum of recipr. factors of 672 = 2 exactly
Sum of recipr. factors of 8128 = 1 exactly perfect!
Sum of recipr. factors of 30240 = 3 exactly
Sum of recipr. factors of 32760 = 3 exactly
Sum of recipr. factors of 523776 = 2 exactly
</pre>
 
=={{header|Groovy}}==
1,707

edits