Quick question - basic mistake probably

This is a discussion on Quick question - basic mistake probably within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying out these programming challenges at www.osix.net , and I'm already stuck on... *sigh* Challenge 2. Well ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    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:
    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;
    }
    Thanks in advance
    Last edited by StickyGoo; 06-22-2006 at 06:14 AM. Reason: [RESOLVED] - Bottom of page.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    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.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    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
    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?!
    Last edited by StickyGoo; 06-22-2006 at 03:12 AM.

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    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.
    Last edited by michaels-r; 06-22-2006 at 03:21 AM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    Talking

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


    That's so satisfying!
    Last edited by StickyGoo; 06-22-2006 at 06:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Quick Question, Basic Programing
    By brian75 in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2008, 07:14 PM
  3. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 04:39 AM
  4. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 09:06 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21