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"
"
"math/big"
)
func main() {
var recip big.Rat
max2 := int64(math.Sqrt(float64(candidate)))
for factor := int64(2); factor <= max2; factor++ {▼
sum.Add(sum, recip.SetFrac64(1, factor))
}▼
sum.Add(sum, recip.SetFrac64(1, f2))
}
}
▲ }
if sum.Denom().Int64() == 1 {▼
perfectstring := ""▼
}
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>
Sum of recipr. factors of 6 = 1 exactly perfect!
Sum of recipr. factors of 28 = 1 exactly perfect!
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}}==
|