# Quick question - basic mistake probably

• 06-22-2006
StickyGoo
Quick question - [RESOLVED]
Hi everyone, I'm trying out these programming challenges at www.osix.net, and I'm already stuck on... *sigh* Challenge 2.

Well here is the question:
Quote:

13 * 2 = 26
x * y = 133745639

Write a program which can work out what the possible values of x and y are.
For this exercise, x and y cannot be 1 and are both whole positive numbers.

Once you have established values for x and y, add them together and submit your answer below.
I read somewhere that people have made brute forcer type programs, so I thought I'd have a go at it.

I came up with this but it doesn't seem to be working, Not that there's any compile errors, but it just seems to be getting stuck somewhere (I tested it on smaller numbers). If anyone can see where I'm going wrong or provide any help that'd be great.

Code:

```#include <iostream> using namespace std; int main() {         int x = 2, y = 2;                         cout << "The brute forcer is now attempting to find the factors\nThis may take some time...";                 while(x * y != 133745639)         {                 for(x=2; x==133745639; x++)                 {                                                 for(y=2; y==133745639; y++)                         {                                                         }                 cout << "\n";                 cout << x << " * " << y << " = " << x*y;                 }         }         cout << "\n-------------------------------\nThe brute forcer has found the factors of 133745639";         cout << "\nThe factors are: " << x << " and " << y;         cout << "\n\nThe sum of these numbers is " << x + y; }```
• 06-22-2006
whiteflags
Your first mistake is here: x==133745639... you keep testing when mathematically the product of x and y are supposed to be the answer. Neither x or y will be the answer on their own.

You are on the right track with your brute forcer, but to find the facters of 133745639 or any other number there is a simple method called trial division.
• 06-22-2006
StickyGoo
Thanks for your help, I made a new one which I think should work.

^The reason I put 'x==133745639' was because I wanted each number to be tested against eachother until it came to the factors, at which point 'while(x * y != 133745639)' would break the loop and I would get my answer... Kind of difficult to explain, but my code below is basically a simpler version.

Only, when I try to compile this one below I'm getting
Quote:

Error E2277 factbrute2.cpp 10: Lvalue required in function main()
*** 1 errors in Compile ***
I have no idea what this message means... (using borland 5.5.1)

Code:

```#include <iostream> using namespace std; int main() {         int x,y= 2;                 cout << "Please wait: \n";                 for(x = 2; (x * y) = 133745638; x++)         {                 y++;         }                 cout << "\n" << x << " * " << y << " = 133745639"; }```
Thanks for telling me about trial division, I'm reading up on it now :)

EDIT::::::::

I just changed it to this:
Code:

```{         int x,y= 2;                 cout << "Please wait: \n";                 for(x = 2; (x * y) == 133745638; x++)         {                 y++;         }                 cout << "\n" << x << " * " << y << " = 133745639"; }```
And it will compile now, I'm testing it at the moment.

EDIT AGAIN:::::::::::::

Ok that reallly messed up... straight away it just said 2 * 2 = 1337blah...

...Why do my programs always screw up?!
• 06-22-2006
michaels-r
Ummmm... you are still not there.
Your for loop will fail (or at least it should do). the condition part is not valid, you are assigning the value 13... to (x*y) which you cannot do and don't want to anyway.

try starting from the following position and you will be on the way:
Code:

```for(x=2; x<=13... /2;x++) {  for(y=2;x*y<13...;y++);     }```
or you could just go through dividing by x and seeing if there is a remainder... using the % operator.
Code:

```for(x=2; x<=13... /2;x++) {  if((13... % x)==0)   etc...```

Not sure if there is a 'required' approach.
• 06-22-2006
StickyGoo
Thanks for your help michaels... I think I'm getting a bit closer.

I've come up with this now:

Code:

```#include <iostream> using namespace std; int main() {         int x = 2;                 cout << "Please wait: \n";                 while((133745639 % x) != 0)         {                 for(x = 2; x <= (133745638 / 2); x++)                 {                                 }         }         cout << x << " = x.";         cout << "\nAnswer = " << (133745639 / x) + x; }```
But I dont think it's going to work, my question is, will the while loop break the for loop even while it is still goin on? or will it not notice x has no remainder because it is embedded in another loop?

...lol, the best I could explain it :S

EDIT::::::::::

Wait a min... this is completely wrong... lemme just do some more thinking/adjustments.

EDIT AGAIN:::::::::::::::::::::

Yay I finally did it!!! thanks for your help michaels-r!

here's the code:
Code:

```#include <iostream> using namespace std; int main() {         int x = 2;                 cout << "Please wait: \n";                 while((133745639 % x) != 0)         {                 x++;         }         cout << "\n" << x << " is one of the factors";         cout << "\n" << (133745639 / x) << " is the other factor.";         cout << "\nThe password is: " << (133745639 / x) + x; }```
:D :D :D

That's so satisfying!