How to make counter for big numbers - go

How to make counter that start from:
10000000000000000000000000000000000000000000000000000000000000000000000000000
and stop when it gets to:
10000000000000000000000000000000000000000000000000000000000000009999999999999
I have this code:
count,one := new(big.Int), big.NewInt(1)
count.SetString("10000000000000000000000000000000000000000000000000000000000000000000000000000",10)
I know this is easy for someone but Im newbi at GoLang so don't be angry if my question is stupid for you :)
Thanks anyway

There you go
package main
import (
"fmt"
"math/big"
)
func main() {
n1 := new(big.Int)
n1.SetString("100000000000000000000000000000000000000000000000000000000", 10)
n2 := new(big.Int)
n2.SetString("100000000000000000000000000000000000000000000000000000009", 10)
one := big.NewInt(1)
for i := n1; i.Cmp(n2) < 0; i.Add(i, one) {
//fmt.Println(i)
}
}
Just fit the right numbers into SetString.

Related

What Did I Miss in Input Process?

I am solving a problem in Hackerearth. Passed all the test cases except 1, showing "Time limit exceeded". What did I really miss in my code?
package main
import(
"fmt"
"strings"
)
func rotateRight(numbers []int, size int, k int) []int {
new_numbers := make([]int, size)
for index, value := range numbers {
new_numbers[(index + k) % size] = value
}
return new_numbers
}
func main() {
var test_case, size, k int
fmt.Scanf("%v", &test_case)
fmt.Scanln()
for i := 0; i < test_case; i++ {
fmt.Scanf("%v %v", &size, &k)
fmt.Scanln()
numbers := make([]int, size)
for i := 0; i<size; i++ {
fmt.Scanf("%v", &numbers[i])
}
result := rotateRight(numbers, size, k)
fmt.Println(strings.Trim(fmt.Sprint(result), "[]"))
}
}
maybe the reason is the way that you read the data, fmt is really slow, try change it with
package main
import (
"bufio"
"os"
)
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
sc.Text()//here you have your data
}
this change will improve the time wasted

Subtraction of large numbers golang

I did not think that I would have to write about it, but nowhere can I find at least an example of subtracting large numbers
So I have two numbers and I want to subtract them, the library says that there is only a limit on memory, however, when I try to subtract two 256-bit numbers I get an error.
Tell me what to do with it and how to solve it?
I began to study Go, everything seems to be so cool, however I constantly encounter similar questions. What's wrong.......
package main
import (
"fmt"
"math/big"
)
func main() {
a := big.NewInt(113792089237316195423570985008687907853269984665640564039457584007908834671645)
b := big.NewInt(20277110887056303803699431755396003735040374760118964734768299847012543114150)
c := big.NewInt(0).Sub(a, b)
fmt.Println("c =", c)
}
Output:
*prog.go:9:18: constant 113792089237316195423570985008687907853269984665640564039457584007908834671645 overflows int64
prog.go:10:18: constant 20277110887056303803699431755396003735040374760118964734768299847012543114150 overflows int64*
I'm using go version go1.12.4 linux/amd64
this code in playground:
https://play.golang.org/p/AY8Z8kkCRdg
Looks like you need to be using big.Int.SetString in your code. Here's a playground link, works there: https://play.golang.org/p/HvEke4g7e8V
For those who don't want to click a link:
package main
import (
"fmt"
"math/big"
)
func main() {
a, _ := new(big.Int).SetString("113792089237316195423570985008687907853269984665640564039457584007908834671645", 10)
b, _ := new(big.Int).SetString("20277110887056303803699431755396003735040374760118964734768299847012543114150", 10)
c := big.NewInt(0).Sub(a, b)
fmt.Println("c =", c)
}
mde.... .......
package main
import (
"fmt"
"math/big"
)
func main() {
i := new(big.Int)
i.SetString("113792089237316195423570985008687907853269984665640564039457584007908834671645", 10)
k := new(big.Int)
k.SetString("20277110887056303803699431755396003735040374760118964734768299847012543114150", 10)
c := big.NewInt(0).Sub(i, k)
fmt.Println("c =", c)
}
https://play.golang.org/p/AuGj9A93FbP

Golang: odd big Int behavior

So I am new to Go and fairly inexperienced with programming in general so I hope I don't get downvoted again for asking stupid questions.
I am working my way through the project euler problems and at problem 25 "1000-digit Fibonacci number" I encountered what seems to be strange behavior. The following is the code I wrote that resulted in this behavior.
package main
import (
"fmt"
"math/big"
)
func main() {
index := 2
l := new(big.Int)
pl := big.NewInt(1)
i := big.NewInt(1)
for {
l = i
i.Add(i, pl)
pl = l
index++
if len(i.String()) == 1000 {
break
}
}
fmt.Println(i, "\nindex: ", index)
}
Naturally this did not generate the correct answer so in the process of determining why I discovered that I had inadvertently discovered a neat way to generate powers of 2. I made the following changes and this did generate the correct result.
package main
import (
"fmt"
"math/big"
)
func main() {
index := 2
l := new(big.Int)
pl := big.NewInt(1)
i := big.NewInt(1)
for {
l.Set(i)
i.Add(i, pl)
pl.Set(l)
index++
if len(i.String()) == 1000 {
break
}
}
fmt.Println(i, "\nindex: ", index)
}
My question is what is happening in the first example that causes each big Int variable to be set to the value of i and why this did not generate an error if this was not the correct way to assign a big Int var value? Is i = l, etc a legitimate big Int operation that is simply incorrect for this situation?
The lines
l = i
and
pl = l
aren't doing what you think they are.
l, pl, and i are pointers, and assigning them to each other copies the pointer value, not the big.Int value.
After executing l = i, l is now the same pointer value as i, pointing to the same big.Int. When you use l.Set(i), it sets l's big.Int value to i's big.Int value, but l and i still point to two separate values.

Big int ranges in Go

Is there a way to loop over intervals between two big int values, x and y, in Go?
for i: = x; i < y; i++ {
// do something
}
Working with big numbers can be kind of clunky because you need to create a big.Int for constants. Other than that, it is a straight forward replacement of each segment of the for statement to one made to deal with big ints.
http://play.golang.org/p/pLSd8yf9Lz
package main
import (
"fmt"
"math/big"
)
var one = big.NewInt(1)
func main() {
start := big.NewInt(1)
end := big.NewInt(5)
// i must be a new int so that it does not overwrite start
for i := new(big.Int).Set(start); i.Cmp(end) < 0; i.Add(i, one) {
fmt.Println(i)
}
}

golang - modulus using math big package

Reading up the documentation - http://golang.org/pkg/math/big/
Mod sets z to the modulus x%y for y != 0 and returns z. If y == 0, a division-by-zero run-time panic occurs. Mod implements Euclidean modulus (unlike Go); see DivMod for more details.
10%4 = 2 but I get 8 with this (using the math/big package to do the same thing) - http://play.golang.org/p/_86etDvLYq
package main
import "fmt"
import "math/big"
import "strconv"
func main() {
ten := new(big.Int)
ten.SetBytes([]byte(strconv.Itoa(10)))
four := new(big.Int)
four.SetBytes([]byte(strconv.Itoa(4)))
tenmodfour := new(big.Int)
tenmodfour = tenmodfour.Mod(ten, four)
fmt.Println("mod", tenmodfour)
}
I most likely got something wrong. Where's the mistake?
It's because SetBytes is not doing what you think! Use SetInt64 instead.
ten := new(big.Int)
ten.SetBytes([]byte(strconv.Itoa(10)))
four := new(big.Int)
four.SetBytes([]byte(strconv.Itoa(4)))
fmt.Println(ten, four)
Result:
12592 52
And indeed, 12592%52 == 8
If you want to use numbers bigger than what int64 lets you manipulate, you can also use the SetString function:
n := new(big.Int)
n.SetString("456135478645413786350", 10)
Just an addition to julienc's answer, if you were to use SetBytes, you have to convert the number to bytes like this :
func int2bytes(num int) (b []byte) {
b = make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(num))
return
}
func main() {
ten := new(big.Int)
ten.SetBytes(int2bytes(10))
four := new(big.Int)
four.SetBytes(int2bytes(4))
fmt.Println(ten, four)
tenmodfour := new(big.Int)
tenmodfour = tenmodfour.Mod(ten, four)
fmt.Println("mod", tenmodfour)
}

Resources