Perfect Numbers

This is a discussion on Perfect Numbers within the C++ Programming forums, part of the General Programming Boards category; This crashes midway through, I'm not exactly sure what I am doing wrong. Code: #include <iostream> #include <conio.h> #include <string> ...

  1. #1
    Shadow12345
    Guest

    Perfect Numbers

    This crashes midway through, I'm not exactly sure what I am doing wrong.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <vector>
    #include <fstream>
    
    using namespace std;
    
    
    int main(void) {
    	ofstream fout;
    	fout.open("PERFECT.txt");
    
    	for(int NumberInQuestion = 0; NumberInQuestion < 1000; NumberInQuestion++) { //cycle through all the niqs
    		for(int Tracker = 0; Tracker <= NumberInQuestion; Tracker++) {	//Cycle through all the numbers that 																		
    			if(NumberInQuestion % Tracker == 0)
    				Tracker = Tracker + Tracker;
    			if(Tracker == NumberInQuestion)
    				fout << Tracker << endl;
    	}
    }
    	fout.close();
    
    	return 0;
    }
    It isn't extremely complicated, brain fart
    Last edited by Shadow12345; 09-04-2002 at 06:16 PM.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    not related to your problem, but why include all the unused header files?
    hello, internet!

  3. #3
    Shadow12345
    Guest
    they'll get used

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Did you try running it through a debugger? It should tell you exactly what line it crashes on.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In n % m, the result is undefined if m = 0.

  6. #6
    Shadow12345
    Guest
    Okay well I got it 'working' but it doesn't actually produce perfect numbers. In case you didn't know a perfect number is one where all of the numbers that divide into it equals the number itself. For instance 6 is a perfect number because 1 + 2 + 3 = 6. I think 28 is another example of a perfect number, so you get the idea. If someone could help me point out logically why my program doesn't output perfect numbers, I would be much happy. Note: the only thing i changed was the 0, so now you can't ever have a divide by zero
    for(int tracker = 0; <<<that is what I changed based on your guys suggestion
    now it is
    for(int tracker = 1;

  7. #7
    Shadow12345
    Guest
    Okay this actually compiles and it 'works', but the numbers aren't perfect, it is odd!


    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <vector>
    #include <fstream>

    using namespace std;


    int main(void) {
    ofstream fout;
    fout.open("PERFECT.txt");

    for(int NumberInQuestion = 1; NumberInQuestion < 1000; NumberInQuestion++) { //cycle through all the niqs
    for(int Tracker = 1; Tracker < NumberInQuestion; Tracker++) { //Cycle through all the numbers that lead up to NIQ
    if(NumberInQuestion % Tracker == 0)
    Tracker = Tracker + Tracker;
    if(Tracker == NumberInQuestion)
    fout << Tracker << endl;
    }
    }
    fout.close();

    return 0;
    }

  8. #8
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47

    Try this,

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <vector>
    #include <fstream>
    
    using namespace std;
    
    
    int main(void) {
    ofstream fout;
    fout.open("PERFECT.txt");
    
    int PerfectNum;
    
    for(int NumberInQuestion = 1; NumberInQuestion < 1000; NumberInQuestion++) 
    {
    	PerfectNum = 0;
    
    	for(int Tracker = 1; Tracker < NumberInQuestion; Tracker++)
    	{
    		if(NumberInQuestion % Tracker == 0)
    			PerfectNum += Tracker;
    		if(PerfectNum == NumberInQuestion)
    		{
    			fout << PerfectNum << endl;
    			break;
    		}
    	}
    }
    fout.close();
    
    return 0;
    }

  9. #9
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Your problem was that Tracker started with a value of one. You need a separate value to keep track of the sum that would start with a value of 0.

  10. #10
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Wait. I think there is another problem. 24 is not a perfect number. It stops too soon.

  11. #11
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Code:
    #include <iostream>
    #include <conio.h>
    #include <string>
    #include <vector>
    #include <fstream>
    
    using namespace std;
    
    
    int main(void) {
    ofstream fout;
    fout.open("PERFECT.txt");
    
    int PerfectNum;
    
    for(int NumberInQuestion = 1; NumberInQuestion < 1000; NumberInQuestion++) 
    {
    	PerfectNum = 0;
    
    	for(int Tracker = 1; Tracker < NumberInQuestion; Tracker++)
    	{
    		if(NumberInQuestion % Tracker == 0)
    			PerfectNum += Tracker;
    	}
    
    	if(PerfectNum == NumberInQuestion)
    		fout << PerfectNum << endl;
    }
    fout.close();
    
    return 0;
    }
    There, I think that should do it. Just moved the 'if(PerfectNum==...' statement out of the loop so it wouldn't print it as perfect if it hadn't gone through all the factors.

  12. #12
    Shadow12345
    Guest
    Oh, that works, ssssweet! What is the largest number you can have? isn't it a long? how can one find out the largest number?

  13. #13
    Registered User mepaco's Avatar
    Join Date
    Aug 2002
    Posts
    47
    Well, without moving to floating point numbers you might have the highest you can get since in a lot of compilers and int is 4 bytes and a long is 4 bytes. You can change it to an unsigned long so you get rid of the negative numbers and extend the positive range. The maximum value of an unsigned long is 4,294,967,295 (I believe). But really anything above 10,000 is going to take a while to compute.

  14. #14
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    If you donīt have a Cray computer you can always look here for large perfect numbers.
    http://mathworld.wolfram.com/PerfectNumber.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

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