# Thread: Russian Peasent

1. well tabstop regarded that i need to discard of my remainders. so idk?

2. ok so ive simplfied the code down to:
Code:
```#include <FPT.h>
int main()
{
double x,y,a;
x=inD();
y=inD();
a=0;
while(floor(y/2)!=1){

if(fmod(x,2)!=0){
a=a+x;
x=x*2;
y=(y/2);

}
else{
x=x*2;
y=(y/2);

}
}
outD(a);
}```
but it still doesnt work! what am i doing wrong

3. Yeah, ignore my post about precision completely. Just use integers all around. Unless you need x to support decimals, in which case y need only be an integer.

4. ok so i made x,y integers..
Code:
```#include <FPT.h>
int main()
{
double a;
int x,y;
x=inD();
y=inD();
a=0;
while(floor(y/2)!=1){

if(fmod(x,2)!=0){
a=a+x;
x=x*2;
y=(y/2);

}
else{
x=x*2;
y=(y/2);

}
}
outD(a);

}```
but im getting 0 in most inputs as answers and 7 for x=7 and y=7.... wth?

5. Well if x is an int, why not just use the &#37; operator. And the previous code didn't address the issue tabstop mentioned.

Now your algorithm has also changed a little bit. What is the result supposed to be? Obviously at some point you are probably just multipling by zero. Or your loop exits too soon. So back to my point about seeing what the result of your loop should be.

6. Since floor(y/2) != 1 is false when y = 2 or 3, you are quitting too soon.

7. well if i enter 3 and 3 it should come out to 9 i presume?

8. Awe man... I thought the bold "or" would be a subtle enough hint.

9. I'm not sure what OutD does, but I don't think you can just output any old integer as a double and get the right number. It needs to be multiplied by 1.0 or something to convert it right first.

I'll stress that peasant multiplication only works with integers anyway so why not just dump the concept of floating point altogether.

10. SO what should my loop condition be?

11. Did you read the post with the algorithm in it (or read your textbook/reference with the algorithm in it)? Did you see what it says right before "then stop"?

12. so ive modified my condtion which i thought would solve the problem but i guess it didnt.
Code:
```#include <FPT.h>
int main()
{
double a;
int x,y;
x=inD();
y=inD();
a=0;
while(floor(y/2)>1){

if(fmod(x,2)!=0){
a=a+x;
x=x*2;
y=(y/2);

}
else{
x=x*2;
y=(y/2);

}
}
outD(a);

}```

13. So for what y is floor(y/2)>1 false?

14. uhhhh... 1? OR 2. i changed it to equal too but it doesnt work

15. 0, 1, 2, and 3.

Since the loop should only stop when y equals 0 (you definitely need to add the last term in when y equals 1) why the heck don't you just compare y to 0?

(Oh, and in your checking-for-odd thing, you need to be checking that y is odd, not x. I don't know how we've managed to miss that all this time.)

Popular pages Recent additions