Thread: Russian Peasant Algorithm

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    4

    Russian Peasant Algorithm

    Hello. I've been working on this for a while for my self study and have managed to get most of it underway. The problem seems to be trying to get the correct total of adding up all the odd numbers on the right-hand side.. When I input the values of 17 and 19 I get the value 0 back which confuses me because I can't see how that's possible... Maybe I just need more experience. Can anyone help me please?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int number;
        int number2;
        int sum = 0;
    
    
        printf("Enter one number");
        scanf("%d",&number);
        printf("Enter one more number.");
        scanf("%d",&number2);
    
    
        do
        {
            printf("%d\t%d\n",number,number2);
            number = number / 2;
            number2 = number2 * 2;
    
    
            if(number2 % 2 == 1)
           {
               sum = sum + number2;
           }
    
    
        }
        while(number!=0);
    
    
        printf("%d",sum);
    
    
    
    
    
    
    
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How to Multiply Using the Russian Peasant Method: 12 Steps
    I see no mention of sum here.

    Only dividing and multiplying by 2.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    This code adds the numbers on the right if the numbers on the right aren't even.

    That is wrong, the numbers on the right are added if the numbers on the left aren't even.

    Also you need to stop at 1, not 0. Likely, using 0 at all is part of your problem.

  4. #4
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    Hmm. For some reason my response didn't go through the first time. I'd like to thank you both for taking the time out of your day to look at this problem. And well, Whiteflags if I omit the 0 then it omits 304 on the right hand-side of the problem. You were right with your explanation too that I need to check if the numbers on the left hand side are even and I tried to fix it but in the end it won't give me the proper sum of 323. That would be the answer to the problem and the only value that I seem to be getting is 304 instead of 323.. If I input 17 and 19 the first value of 19 is what I want to add to the value of 304 to give me the answer but it still fails no matter what I try.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    It's pretty easy:
    Code:
    #include <stdio.h>
    
    int main() {
        int a, b, sum = 0;
        printf(">>> ");
        scanf("%d %d", &a, &b);
        while (a > 0) {
            if (a % 2 == 1)
                sum += b;
            a /= 2;
            b *= 2;
        }
        printf("%d\n", sum);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 05-22-2009, 07:03 AM
  2. Russian Peasent
    By Lucid15 in forum C Programming
    Replies: 34
    Last Post: 09-30-2008, 12:07 PM
  3. Peasant's Quest Walkthrough
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-25-2004, 06:46 AM
  4. Stuck on Peasant's Quest
    By sean in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-16-2004, 10:00 AM
  5. Russian stuffffffffff
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-22-2004, 01:21 PM

Tags for this Thread