Beginner – Having an issue with simple multiplication algorithm

Beginner – Having an issue with simple multiplication algorithm

Hello, I am learning Go and I started by recreating the simple 'Egyptian Multiplication Algorithm', also known as the 'Russian Peasants Algorithm'

my code:

package main import "fmt" func russian_peasants_algorithm(x, y int) (z int) { for x > 0 { if x % 2 == 0 { z = z + y } y = y << 1 x = x >> 1 } return } func main() { fmt.Println(russian_peasants_algorithm(2, 3)) } 

This is printing the number 3 instead of 6. It seems like the if statement isn't firing properly? I finished A Tour of Go and starting working on my local machine but can't figure out what the issue on this code is. I downloaded gore to use as a REPL and stepped through the code line by line and it seems to be doing what it needs to be.

Thank you in advance, I am also wondering what types of projects I can build to learn Go, where does Go really shine, and what type of programs are fun to build with Go?

Submitted August 13, 2017 at 07:01AM by Jumballaya
via reddit


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s