Results 1 to 6 of 6

Thread: Russian Peasant Calculation Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Scotland
    Posts
    21

    Question Russian Peasant Calculation Problem

    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

    Cheers

    CaptainChainsaw


    Here is what I've got so far:

    #include <iostream>

    using namespace std;

    void main() {

    int a, b, sum;

    cout << "Enter a number for a: ";
    cin >> a;

    cout << "Enter a number for b: ";
    cin >> b;


    while(b%2>=1) {

    sum+=a*2;


    if(b%2 == 0){
    sum-=a*2;

    }

    }


    cout << sum << " is the answer!!!" ;

    }

  2. #2
    New Member
    Join Date
    Jan 2002
    Posts
    13
    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;
    }
    Attached Files Attached Files

  3. #3
    New Member
    Join Date
    Jan 2002
    Posts
    13

    Wink correction

    hi,
    I develop the code likes this.....

    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;
    }

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Scotland
    Posts
    21
    Thank you ...... much appreciated, I'll go and have a look at it just now.

    CaptainChainsaw

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Location
    Scotland
    Posts
    21
    I managed to get it going, here's the code, thanks for the help!!

    // Start of Program

    #include <iostream>

    using namespace std;

    void main() {

    int a, b, sum=0;

    cout << "Enter a number for a: ";
    cin >> a;

    cout << "Enter a number for b: ";
    cin >> b;

    while(b!=1) {
    if(b%2>=1){
    sum+=a;
    }
    b=b/2;
    a=a*2;
    }

    sum+=a;
    cout << "\n\nThe answer is " << sum << "\n\n";
    }
    // end Of program

    CaptainChainsaw

  6. #6
    New Member
    Join Date
    Jan 2002
    Posts
    13
    hi,
    I think if you want to write multiply algorithim you can not use multiply.... so shifting is much better in here also more faster....
    yilmaz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width