Thread: Please help a novice

  1. #1
    Unregistered
    Guest

    Unhappy Please help a novice

    Hi,
    Pleas help me to solve this ............I tried...but now ...I am feeling really tired.,my brain is standstill.....
    HELPPPPPP!

    The question is to find perfect number between 1 to 100.Well,who doesnt know perfect no.is the sum of its factors ,including 1 but not number itself.For eg .6 =1+2+3 is perfect no.
    The problem is of finding perfect number from 1 to 100.

    I have almost completed it (I assume....
    But I am stucked on the last part.
    I am checking the totalof factors with original number ..to see its perfect number...
    But its not showing the return value....???
    Another probelm is of scrolling of the screen fast.
    I just dont know the syntax of pausing it..and where in my prog should I use it????

    thanks in advance.......Help!!
    My code :


    //function perfect//

    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ios;

    int perfect();//funcn prtotype

    int main()
    {

    perfect();//calling funcn
    return 0;
    }
    int perfect ()//funcn defn
    {
    int quo,rem,result,counter,count;
    quo=0;
    rem=0;
    result=0;
    for(count=1;count<=100;count++)//nos from 1 to 100
    {

    for(counter=1;counter<count-1;counter++) //nos for dividing it with count
    {
    quo=count/counter;
    rem=count%counter;

    if (rem==0)//checking for remainder is 0 ans showing the factors
    {
    cout<<count<<"its factors:"<<counter<<endl;
    result=result+counter;//storing the total of factors in result

    }


    }
    cout<<"total of factors"<<result;
    cout<<endl;

    result=0;
    }



    if(count==result) //problem lies here.......??//
    {
    return count;
    }

    }
    thanks in advance...

  2. #2
    Unregistered
    Guest
    Make perfect check if only the inputted number is a perfect number. Then, in the main, create a loop going through all possible values. This will make the program a lot easier to make.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    crap, it didn't sign me in for some reason.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    try this:
    Code:
    #include <iostream.h>
    
    int main(void)
    {
    	for(int i = 2; i <= 10000; i++)
    	{
    		int sum = 0;
    
    		for(int j = 1; j < i; j++)
    		{
    			if(i % j == 0)
    			{
    				sum += j;
    			}
    		}
    
    		if(sum == i)
    		{
    			cout << i << " is a perfect number\n";
    		}
    	}
    
    	return(0);
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    here's a faster version... but it's not particularly nice

    Code:
    #include <iostream.h>
    #include <math.h>
    #include <memory.h>
    
    #define AMOUNT 10000
    
    int main(void)
    {
    	int factors[1000]; // buffer for storing factors;
    	int flags[AMOUNT];
    
    	for(int i = 2; i <= AMOUNT; i++)
    	{
    		memset(flags, 0, sizeof(flags));
    		memset(factors, 0, sizeof(factors));
    
    		int squareRoot = (int)sqrt((double)i);
    		int factor = 0;
    		int sum = 0;
    		int j = 0;
    
    		for(j = 2; j <= squareRoot; j++)
    		{
    			if(i % j == 0)
    			{
    				if(!flags[j])
    				{
    					flags[j]++;
    					factors[factor++] = j;
    				}
    				if(!flags[i / j])
    				{
    					flags[i / j]++;
    					factors[factor++] = i / j;
    				}
    			}
    		}
    
    		j = 0;
    		while(factors[j] != 0)
    		{
    			sum += factors[j++];
    		}
    
    		if(sum + 1 == i)
    		{
    			cout << i << " is a perfect number\n";
    		}
    	}
    
    	return(0);
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    Unregistered
    Guest

    Talking Thanks

    Thanks mate....I am recharged now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2005, 10:00 PM
  2. Novice trying to learn C++
    By dead in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2003, 09:25 PM
  3. Rtfm
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 03-13-2003, 05:19 PM
  4. help for a C novice
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-02-2002, 09:49 PM
  5. Novice needing help PLEASE!!!!!
    By Nick Dillon in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 02:21 PM