-
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
-
not related to your problem, but why include all the unused header files?
-
-
Did you try running it through a debugger? It should tell you exactly what line it crashes on.
-
In n % m, the result is undefined if m = 0.
-
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;;)
-
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;
}
-
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;
}
-
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.
-
Wait. I think there is another problem. 24 is not a perfect number. It stops too soon.
-
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.
-
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?
-
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.
-
If you donīt have a Cray computer you can always look here for large perfect numbers.
http://mathworld.wolfram.com/PerfectNumber.html