Hi there, I'm trying to write a program to solve the multiplication of two numbers using the Russian Peasant method. If you don't know what this method is you will find out @ http://mathforum.org/dr.math/faq/faq.peasant.html
Unfortnately when I iterate round the loop the test condition needs to be changed to give a correct answer. Is there some way of making the test condition in the while loop dynamic so that the program will display the correct result ?
As far as I know this problem should be solvable by using a do - while loop & an if statement.
Any help/advice would be greatly appreciated.
I know that this code will just create an infinte loop because there is just one value of 'b' and its just running the same test over and over ..... etc
hi,
I wrote this code I think this is helpfull for you...
/* yilmaz saridemir [email protected]
*
#include<stdio.h>
unsigned long russianPeasant(unsigned long ,unsigned long );
void main(void){
printf("result=%u",russianPeasant(16,23));
}
unsigned long russianPeasant(unsigned long x,unsigned long y){
unsigned long result=0;
while (y>=1){
if ((y%2)==1) {result+=x;}
x<<=1;//multiple 2
y>>=1;//divide 2
}
return result;
}
unsigned long russianPeasant(unsigned long x,unsigned long y){
unsigned long result=0;
while (y>=1){
if (y&1) {result+=x;}
x<<=1;//multiple 2
y>>=1;//divide 2
}
return result;
}