16 Replies  4526 Views  Last Post: 02 November 2010  11:47 AM
#1
divisor of an integer
Posted 31 October 2010  05:05 PM
The proper divisors of an integer n are the positive integer divisors whose values are less than n. A positive integer is said to be a deficient, perfect, or abundant number if the sum of its proper divisors is, respectively, less than, equal to, or greater than the number. For example, 8 is deficient because its proper divisors are 1, 2, and 4, and 1 + 2 + 4 < 8; The integer 6 is perfect because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6; The integer 12 is abundant because its proper divisors are 1, 2, 3, 4, and 6, and
1 + 2 + 3 + 4 + 6 > 12. Write a program that classifies an integer n as being deficient, perfect, or abundant, for the following values of n:
DO NOT write a loop to do the numbers for one group of numbers and then simply copy that loop for the other two groups of numbers. Instead, use a loop that controls which group of numbers to use; That is, put your loop that determines whether the numbers within a group are deficient, perfect, or abundant inside a loop that determines which group of numbers is being considered. Use the below range of numbers for your testing.
n = 90 to 100 inclusive
n = 350 to 360 inclusive
n = 8120 to 8130 inclusive
Display your results in a tabular format similar to the following example:
Number classification
8 deficient
6 perfect
12 abundant
Hi, everyone im back this week with a small math question relating with this lab I have. Sorry im not a math wiz =(
Moving on to my question. I understand that this program require me finding all the divisor of a program but I cannot think of the math algorithm I would use to find it. can anyone assist this math idiot?
Replies To: divisor of an integer
#2
Re: divisor of an integer
Posted 31 October 2010  05:27 PM
#3
Re: divisor of an integer
Posted 31 October 2010  05:45 PM
what would I have to write in the for loop to get all the divisor for a integer 90100
int main() { int r1 = 90; int r2 = 350; int r3 = 8120; int Number = 0 int Menu_Input = 0; int Sum = 0; cout<<"Please pick a range from the following menu to determine"<<endl; cout<<"If the number in that range are either:"<<endl; cout<<"perfect, abundant, or deficient."<<endl<<endl; cout<<"1. 90 to 100"<<endl; cout<<"2. 350 to 360"<<endl; cout<<"3. 8120 to 8130"<<endl<<endl; cin>>Menu_Input; cout<<"Number Classification"<<endl; cout<<"_____________________________________"<<endl; if (Menu_Input == 1) { for (r1; r1 >= 100; r1++) { cout<<r1/ } }
#4
Re: divisor of an integer
Posted 31 October 2010  05:52 PM
#5
Re: divisor of an integer
Posted 31 October 2010  06:15 PM
#6
Re: divisor of an integer
Posted 31 October 2010  06:24 PM
int r[3] = {90,350,8120}; int sum; for (int n = 0; n < 3; n++) { for (int i = r[n]; i <= r[n]+10; i++) { sum = 0; for (int j = 1; j <= i/2; j++) {
This post has been edited by CTphpnwb: 31 October 2010  06:27 PM
#7
Re: divisor of an integer
Posted 01 November 2010  07:18 PM
using namespace std; int main() { int r1 = 90; int r2 = 350; int r3 = 8120; int Number = 0; int Menu_Input = 0; int Sum = 2; int x = 0; char deficient = 'd'; char perfect = 'p'; char abundant = 'at'; cout<<"Please pick a range from the following menu to determine"<<endl; cout<<"If the number in that range are either:"<<endl; cout<<"perfect, abundant, or deficient."<<endl<<endl; cout<<"1. 90 to 100"<<endl; cout<<"2. 350 to 360"<<endl; cout<<"3. 8120 to 8130"<<endl<<endl; cin>>Menu_Input; cout<<"Number Classification"<<endl; cout<<"_____________________________________"<<endl; if (Menu_Input == 1) { for (int i = r1; i <= r1+10; i++) { Sum = (x % r1); // supposedly adds up the divisor // for r1 for (int j = 1; j <= i/2; j++) { if (Sum < r1) { cout<<i<<" "<<deficient<<endl; } else if (Sum > r1) { cout<<i<<" "<<abundant<<endl; } else { cout<<i<<" "<<perfect<<endl; } break; } } }
#8
Re: divisor of an integer
Posted 01 November 2010  07:23 PM
#9
Re: divisor of an integer
Posted 01 November 2010  07:58 PM
for (int n = 0; n < 3; n++) {does.
for (int i = r[n]; i <= r[n]+10; i++) {increment the numbers from 90to100 etc.
for (int j = 1; j <= i/2; j++) {increment by 1
#10
Re: divisor of an integer
Posted 01 November 2010  08:05 PM
You want to check 11 numbers in each group, right? Loop 2 starts with the first number (ie: 90) and goes to the eleventh number (ie, 100)
You need to check all numbers from 1 to half of the current number (see Loop 2 above) to see if they divide evenly into that number. Loop 3 allows you to do that.
This post has been edited by CTphpnwb: 01 November 2010  08:07 PM
#11
Re: divisor of an integer
Posted 01 November 2010  08:34 PM
#12
Re: divisor of an integer
Posted 01 November 2010  10:46 PM
int main() { int r1 = 90; int r2 = 350; int r3 = 8120; int Number = 0; int Menu_Input = 0; int Sum = 1; int j = 2; char deficient = 'd'; char perfect = 'p'; char abundant = 'a'; cout<<"Please pick a range from the following menu to determine"<<endl; cout<<"If the number in that range are either:"<<endl; cout<<"perfect, abundant, or deficient."<<endl<<endl; cout<<"1. 90 to 100"<<endl; cout<<"2. 350 to 360"<<endl; cout<<"3. 8120 to 8130"<<endl<<endl; cin>>Menu_Input; cout<<"Number Classification"<<endl; cout<<"_____________________________________"<<endl; if (Menu_Input == 1) { for(int i=r1; i <= r1+10; i++) { cout <<i<<": "; for(j = 2; j < i; j++) if((i%j) == 0) Sum = j + Sum; if (Sum < r1) { cout<<Sum<<""; cout<<deficient<<""; cout<<endl; } else if (Sum > r1) { cout<<Sum<<""; cout<<abundant<<""; cout<<endl; } else { cout<<Sum<<""; cout<<perfect<<""; cout<<endl; } } }
#13
Re: divisor of an integer
Posted 02 November 2010  06:15 AM
Instead of this:
if((i%j) == 0) Sum = j + Sum;
use this:
if((i%j) == 0) { Sum = j + Sum; }
#14
Re: divisor of an integer
Posted 02 November 2010  10:05 AM
CTphpnwb, on 02 November 2010  05:15 AM, said:
Instead of this:
if((i%j) == 0) Sum = j + Sum;
use this:
if((i%j) == 0) { Sum = j + Sum; }
unfortunately, after I fixed that int Sum would still do this
90 144
91 164
92 239
93 273
94 322
95 346
96 501
97 501
98 573
99 629
100 745
when it should give me
90 144
91 21
92 76
93 35
94 50
95 25
96 156
97 1
98 73
99 57
100 117
#15
Re: divisor of an integer
Posted 02 November 2010  10:23 AM
