divisor of an integer

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3571 Views - Last Post: 02 November 2010 - 11:47 AM Rate Topic: -----

#1 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

divisor of an integer

Posted 31 October 2010 - 05:05 PM

Problem Statement
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?

Is This A Good Question/Topic? 0
  • +

Replies To: divisor of an integer

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 31 October 2010 - 05:27 PM

If a number X is a divisor of Y, then X will divide evenly into Y. In other words the remainder of Y/X will be zero, or X % Y == 0
Was This Post Helpful? 0
  • +
  • -

#3 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 31 October 2010 - 05:45 PM

I meant if we had a program like this

what would I have to write in the for loop to get all the divisor for a integer 90-100


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



Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 31 October 2010 - 05:52 PM

And I told you the answer! You just need to code it properly.
Was This Post Helpful? 0
  • +
  • -

#5 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 31 October 2010 - 06:15 PM

So y would be my r1 and x would be my divisor. So I would have to mod r1 till it goes to zero? What would I mod r1 with?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 31 October 2010 - 06:24 PM

Maybe this will get you pointed in the right direction.
	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

Was This Post Helpful? 0
  • +
  • -

#7 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 01 November 2010 - 07:18 PM

Thx for the for loop example there, but im still having trouble with the divisor.

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

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 01 November 2010 - 07:23 PM

You didn't use the loops I gave you! They're an important starting point. Tell me why they are, or why you think they aren't.
Was This Post Helpful? 0
  • +
  • -

#9 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 01 November 2010 - 07:58 PM

Im not sure what
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
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 01 November 2010 - 08:05 PM

You want to check 3 groups of numbers, correct? Loop 1 provides the index for loop 2.

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

Was This Post Helpful? 1
  • +
  • -

#11 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 01 November 2010 - 08:34 PM

Oh I understand what you were trying to tell me now, but for loop 3 I have to find the divisor of the the number within the range not just if they can be divided evenly into it. However I get the idea you were telling me to do thank you so much.
Was This Post Helpful? 0
  • +
  • -

#12 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 01 November 2010 - 10:46 PM

What would I have to do to stop Sum from adding up from all number in the range and just make it just add up the divisor for that number.


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

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 02 November 2010 - 06:15 AM

This is why you should always use braces.
Instead of this:
                if((i%j) == 0) 
         
                Sum = j + Sum;


use this:
                if((i%j) == 0) 
         		{
                	Sum = j + Sum;
				}

Was This Post Helpful? 0
  • +
  • -

#14 Nitaru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-October 10

Re: divisor of an integer

Posted 02 November 2010 - 10:05 AM

View PostCTphpnwb, on 02 November 2010 - 05:15 AM, said:

This is why you should always use braces.
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
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: divisor of an integer

Posted 02 November 2010 - 10:23 AM

You need to reset sum to 0 every time you restart the loop.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2