I’m struggling in getting my func returning a big number.

The problem I’m trying to resolve is this Euler problem

package main
import (
"fmt"
"math"
)
var result float64 = 0
func recursive(a float64) float64 {
if a == 1000 {
return math.Pow(a, a)
}
result := math.Pow(a, a) + recursive(a+1)
return result
}
func main() {
number := recursive(1)
fmt.Printf("%f \n", math.Mod(number, 10))
fmt.Println(number)
fmt.Printf("%T", number)
}

If you try to run the file you will realize that the number returns an +Inf as a result.
I also tried another version to use the Big package

But seems not to work

var result = big.NewFloat(0)
func recursive(a float64) *big.Float {
if a == 1000 {
return big.NewFloat(math.Pow(a, a))
}
result := result.Add(big.NewFloat(math.Pow(a, a)), recursive(a+1))
return result
}
func main() {
number := recursive(1)
fmt.Println(number)
fmt.Printf("%T", number)
}

Any idea how to work on this? The language should enable us to work with big numbers for big operations. That’s how computers are supposed to make our life easier isn’t?

This still requires a float to be able to hold the result of a to the power of a, which may not be the case. You should probably stick to the math.Big operations completely.

(Being number a *big.Float) Is there any way of getting the last digits? I did not found a way to get % to work in here, moreover I’ve tried to convert my *big.float into an integer but I want all decimals to become integers. Any suggestions on this?

This is the snippet so far

import (
"fmt"
"math/big"
)
var result = big.NewFloat(0)
func recursive(a int) *big.Float {
var bigA *big.Float = big.NewFloat(float64(a))
if a == 1000 {
return bigA.SetMantExp(bigA, a)
}
result := result.Add(bigA.SetMantExp(bigA, a), recursive(a+1))
return result
}
func main() {
number := recursive(1)
fmt.Printf("%.0f\n", number)
}

Hi Marcos,
you don’t need big numbers for tis problem.
Here is my solution for this problem:

package main
import (
"fmt"
"strconv"
)
const k = 10000000000
func modPower(n int) int {
var res = 1
for i:=1; i <= n; i++ {
res = (res * n) % k
}
return res
}
func main() {
var euler = 0
for j := 1; j<= 1000; j++ {
euler = (euler + modPower(j)) % k
}
fmt.Println(strconv.Itoa(euler))
}

I meant to get the last digits of my var number.
I did not realize we have a Text method under *big.Float
So finally I could manage to get the last 10 digits like this