perfectNumber

Can't figure out level-2 to level-4>>>
Level 1: Small perfect numbers Begin by writing a program that prints perfect numbers (those with a badness of 0). Separate the output of each number by a single space. Read the limiting value as a command line argument. For example, quitegood 100 should print 6 28.For this level, you can assume that the limiting value will not exceed 10,000.

Level 2: Small near-perfect numbers Extend the program so that the badness limit can be specified as a second command line argument. For example,quitegood 100 3 should print 2 3 4 6 8 10 16 18 20 28 32 64.

Level 3: Large numbers Extend the program so that it will work efficiently for values up to at least 1,000,000. For example, quitegood 1000000
1 should print 2 4 6 8 16 28 32 64 128 256 496 512 1024 2048 4096 8128 8192 16384 32768 65536 131072 262144 524288.

Level 4: Convert either way Extend the program so that a negative badness value causes the program to output a count of the quite good numbers
rather than the numbers themselves. For example, quitegood 1000000 -1 should print 23 because there are 23 numbers less than 1,000,000 with a badness no more than 1.

//code
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

bool checkFactor(int ouFactor, int ourNumber) {
//Here we use if statement to check the number is a factor and if so we return true else we return false
if (ourNumber % ouFactor == 0) {
return true;
} else {
return false;
}
}

int badnessValueCalc(int ourNumbernum) {
int ourTotal = 1;

for (int ouFactor = 2; ouFactor < ourNumbernum; ouFactor++) {
if (checkFactor(ouFactor, ourNumbernum)) {
ourTotal += ouFactor;
}
}

int badnessValue = ourNumbernum - ourTotal;
return abs(badnessValue);
}

void display(int n, int b) {

int count = 0;
for (int k = 2; k < n; k++) {
int bValue = badnessValueCalc(k);
if (b < 0) {
if (bValue <= abs(b))
count++;

} else if (bValue <= (b)) {
cout << k << " ";
}

}
if (b < 0)
cout << count << " ";
cout << endl;
}

//Level 1 passed
int main(int argc, char *argv[]) {
int n=argc>1? atoi(argv[3]):100;
int b = 0;

display(n, b);
cout << "\n\n";




//Level 2 quitegood 100 3

n = 100;
b = 3;

display(n, b);
cout << "\n\n";


//Level 3 quitegood 1000000 1

n = 1000000;
b = 1;

display(n, b);
cout << "\n\n";


//Level 4 quitegood 1000000 -1

n = 1000000;
b = -1;
display(n, b);
cout << "\n\n";
}
//End code
When posting code, please use code tags so that the code is readable!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

bool checkFactor(int ourFactor, int ourNumber) {
	return ourNumber % ourFactor == 0;
}

int badnessValueCalc(int ourNumbernum) {
	int ourTotal = 1;

	for (int ouFactor = 2; ouFactor < ourNumbernum; ouFactor++)
		if (checkFactor(ouFactor, ourNumbernum))
			ourTotal += ouFactor;

	int badnessValue = ourNumbernum - ourTotal;
	return abs(badnessValue);
}

void display(int n, int b) {
	int count = 0;

	for (int k = 2; k < n; k++) {
		int bValue = badnessValueCalc(k);

		if (b < 0) {
			if (bValue <= abs(b))
				count++;

		} else if (bValue <= (b))
			cout << k << " ";
	}

	if (b < 0)
		cout << count << " ";

	cout << '\n';
}

//Level 1 passed
int main(int argc, char* argv[]) {
	int n = argc > 1 ? atoi(argv[3]) : 100;
	int b = 0;

	display(n, b);
	cout << "\n\n";

	//Level 2 quitegood 100 3
	n = 100;
	b = 3;

	display(n, b);
	cout << "\n\n";

	//Level 3 quitegood 1000000 1
	n = 1000000;
	b = 1;

	display(n, b);
	cout << "\n\n";

	//Level 4 quitegood 1000000 -1
	n = 1000000;
	b = -1;
	display(n, b);
	cout << "\n\n";
}


Note checkFactor() can be simplified.
Last edited on
Hello Wali23,

As I looked over the program a few things I noticed. The comments cover most of the problems.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <math.h>  // <--- Should use "<cmath>" for a C++ program.
#include <string>

using namespace std;  // <--- Best not to use.

bool checkFactor(int ouFactor, int ourNumber)
{
    //Here we use if statement to check the number is a factor and if so we return true else we return false
    if (ourNumber % ouFactor == 0)
    {
        return true;
    }
    //else
    //{
    //}
    return false;
}

int badnessValueCalc(int ourNumbernum)
{
    int ourTotal = 1;

    for (int ouFactor = 2; ouFactor < ourNumbernum; ouFactor++)
    {
        if (checkFactor(ouFactor, ourNumbernum))
        {
            ourTotal += ouFactor;
        }
    }

    int badnessValue = ourNumbernum - ourTotal;

    return abs(badnessValue);
}

void display(int n, int b)
{
    int count = 0;

    for (int k = 2; k < n; k++)
    {
        int bValue = badnessValueCalc(k);

        if (b < 0)
        {
            if (bValue <= abs(b))
                count++;

        }
        else if (bValue <= (b))
        {
            cout << k << " ";
        }
    }

    if (b < 0)
        cout << count << " ";

    cout << '\n';
}

//Level 1 passed
int main(int argc, char *argv[])
{
    int n{ argc > 1 ? atoi(argv[3]) : 100 };
    int b{};

    display(n, b);

    cout << "\n\n";

    //Level 2 quitegood 100 3
    n = 100;
    b = 3;

    display(n, b);

    cout << "\n\n";

    //Level 3 quitegood 1000000 1
    n = 1000000;
    b = 1;

    display(n, b);

    cout << "\n\n";

    //Level 4 quitegood 1000000 -1
    n = 1000000;
    b = -1;

    display(n, b);

    cout << "\n\n";

    return 0;  // <--- Not required, but makes a good break point for testing. Can be removed later.
}

Foe line 2 there are many C header files that start with "c" and have no extension. These should be used in a C++ program. For a list f these see: http://www.cplusplus.com/reference/ or take the "Reference" link in the upper left corner of the page.

In the "checkFactor" function should the if statement evaluate to true you leave the function with the return statement. Should the if statement evaluate to false you bypass the if statement and the only thing left is to return false. The "else" statement is not needed. Think about it.

In "main", line 66, if "argc" == 2 this is greater than 1, but that means tha command line only contains the program name, "argv[0]", an 1 argument, "argv[1]". So "argv[3]" does not exist and the most that you would have is "argv[2] for 2 numbers which would set "argc" to 3.

Other than removing some blank lines that you really do not need and adding a couple so that it reads better that is what I see.

I have yet to load this up and compile it to see if there are any other errors that might come up.

Andy
checkFactor() is one line of code - no tests. See mine above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

int main( int argc, char * argv[] )
{
   int n = 1000000;  // atoi( argv[1] );
   int g = 1;        // atoi( argv[2] );
   
   int * sumProperFactors = new int[1+n]{};
   for ( int i = 1; 2 * i <= n; i++ )
   {
      for ( int j = 2 * i; j <= n; j += i ) sumProperFactors[j] += i;
   }
   
   int numGood = 0;
   int absg = abs( g );
   for ( int i = 2; i <= n; i++ )       // No idea why you don't accept 1
   {
      if ( abs( sumProperFactors[i] - i ) <= absg )
      {
         numGood++;
         if ( g >= 0 ) cout << i << " ";
      }
   }
   if ( g < 0 ) cout << numGood;
   cout << '\n';
   delete [] sumProperFactors;
}



2 4 6 8 16 28 32 64 128 256 496 512 1024 2048 4096 8128 8192 16384 32768 65536 131072 262144 524288 
Last edited on
Topic archived. No new replies allowed.