Help with program using multiplication russian method

Hi, i want use multiplication russian method with form recursive in c. Look link of multiplication russian:
http://mathforum.org/dr.math/faq/faq.peasant.html

Look my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdlib.h>
#include <stdio.h>


void russa(long *x, long *y, long *res)
 {
    if(*y % 2 != 0){
       *x *= 2;
       *y /= 2;
       *res += (*x + *y);
    }

    if(y != 1){
        printf("\nres = %d x = %d y = %d\n", *res, *x, *y);
        system("pause");
        russa(x, y, res);
    }

}

int main(){

long x, y, res = 0;
long *px, *py, *pz;
printf("Digite um numerurzin ae: ");
scanf("%d", &x);
printf("Agora digite mais um e ta bao: ");
scanf("%d", &y);
px = &x; py = &y; pz = &res;
russa(px, py, pz);
printf("%d\n", res);

return 0;
 }
When your code doesn't compile, it is customary to let it be known and supply the generated errors. Nobody here can read minds over the internet, so supplying as much information as possible means you are more likely to get cogent help.

y is a pointer. It doesn't make much sense to compare the address held by y to 1 on line 13.

There is no good reason for you to be using pointers for x and y.

Your logic is wrong. Doubling x and halving y should not be predicated on the oddness of *y. Nor does the oddness of *y indicate whether or not the next set of values contributes to the result. Finally, *y should never be added to the result.

Here's a C++ solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iomanip>
#include <iostream>

using value_type = long long;

value_type multiply(value_type a, value_type b, value_type partial_result=0)
{
    if (b % 2 == 1)
        partial_result += a;

    if (b == 1)
        return partial_result;

    return multiply(a*2, b/2, partial_result);
}

int main()
{
    for (unsigned i = 1; i < 21; ++i)
    {
        for (unsigned j = 1; j < 21; ++j)
            std::cout << std::setw(4) << multiply(i, j);
        std::cout << '\n';
    }
}


or without worrying about tail recursion:
1
2
3
4
5
6
7
value_type multiply(value_type a, value_type b)
{
    if (b == 1)
        return a;

    return (b % 2 == 1 ? a : 0) + multiply(a * 2, b / 2);
}
Last edited on
Your algorithm is off.

Remember, you get two numbers, x and y. (They do not need to be pointers to an x or y.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
long russa(long x, long y)
{
  long res;

  ...

  return res;
}

int main()
{
  long x, y, res;

  printf("Digite um numerurzin ae: ");
  scanf("%ld", &x);

  printf("Agora digite mais um e ta bao: ");
  scanf("%ld", &y);

  res = russa(x,y);
  printf("%ld * %ld = %ld\n", x, y, res);
}


Peasant multiplication  
The way it works is this:

    while x ≠ 0:
      if x is odd: add y to the result
      x = x ÷ 2
      y = y * 2

It will always terminate correctly, so you don't need to check anything. (x will always be zero and y will always be something large.)

If you wish to optimize it (and this is entirely optional for your homework), make sure that x ≤ y before you start (swap them if they aren't).

Hope this helps.
Topic archived. No new replies allowed.