Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,236 questions

56,139 answers

573 users

How to calculate the Ethiopian multiplication in Rust

1 Answer

0 votes
/*
Example: 57 * 18

    57      18
Halving the first column:

    28      18
    14
     7
     3 
     1
Doubling the second column:

    28       36
    14       72
     7      144
     3      288
     1      576
Remove rows whose first cell is even:

             18
    28       36 x 
    14       72 x
     7      144
     3      288
     1      576
Sum the remaining numbers in the right-hand column:
             18
    28        - 
    14        - 
     7      162
     3      288
     1      576
           ====
           1026
*/

fn ethiopian_multiply(mut a: i32, mut b: i32) -> i32 {
    let mut sum = 0;

    while a > 0 {
        if a % 2 == 1 {   // keep only odd rows
            sum += b;
            println!("sum = {}", sum);
        }

        a /= 2;           // halve left side
        b *= 2;           // double right side
        println!("a = {} b = {}", a, b);
    }

    sum
}

fn main() {
    let a = 57;
    let b = 18;

    println!("{}", ethiopian_multiply(a, b));
}



/*
run:

sum = 18
a = 28 b = 36
a = 14 b = 72
a = 7 b = 144
sum = 162
a = 3 b = 288
sum = 450
a = 1 b = 576
sum = 1026
a = 0 b = 1152
1026

*/

 



answered Mar 17 by avibootz
...