Thread: Perfect Number - faster Algo Pls..

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Perfect Number - faster Algo Pls..

    I created a problem and solved it but i noticed that the outputting is kinda slow. Can someone here make a faster algo for this problem than mine pls?

    This problem will output all perfect numbers between 1 and int max.

    A perfect number is an integer that is equal to the sum of all of its factors other than itself..

    e.g.
    1 + 2 + 3 = 6

    other perfect numbers are 28 and 496


    PHP Code:
    #include<stdio.h>
    #include<conio.h>

    int main()
    {
        
    long numctr;
        
    long curtotalmax;
        
    clrscr();

        
    printf"Input:" );
        
    scanf"%ld", &max );
        
    printf"\nOutput:\n\n" );

        for( 
    num 1num maxnum++ ){
            for( 
    ctr 1curtotal 0ctr numctr++ ){
                if( ( 
    num ctr ) ==  )
                    
    curtotal += ctr;
            }

            if( 
    curtotal == num )
                
    printf"%ld\n"num );
        }

        
    getch();
        return 
    0;

    Last edited by loko; 08-13-2005 at 04:32 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A way to speed things up would be to use an optimised method of finding prime factors of the number. All factors of a number can be computed by as the product of some of it's prime factors (raised to some power). Look up sieve of erastothenes for a method of computing primes.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Or you could just write:

    Code:
    if (max >= 6) {
        puts("6");
        if (max >= 28) {
            puts("28");
            if (max >= 496) {
                puts("496");
                if (max >= 8128) {
                    puts("8128");
                    if (max >= 33550336) {
                        puts("33550336");
                    }
                }
            }
        }
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a lookup table. :P


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Rashakil Fol
    Or you could just write:

    Code:
    if (max >= 6) {
        puts("6");
        if (max >= 28) {
            puts("28");
            if (max >= 496) {
                puts("496");
                if (max >= 8128) {
                    puts("8128");
                    if (max >= 33550336) {
                        puts("33550336");
                    }
                }
            }
        }
    }
    :-) Too bad this approach will not find any perfect number over 33550336 .....

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    The 'long' datatype has an upper limit of 2147483647, in most situations, and no perfect numbers lie between 33550336 and 2147483647.

  8. #8
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    hey, maybe the man wants to store the number in a list. then there would be prime numbers larger than 33550336.

    anyway, erathostene wouldn't work for big numbers. you need an array of trunc(sqrt(x)) elements that can store atleast trunc(sqrt(x)), where x is the number to be tested.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    hey, maybe the man wants to store the number in a list. then there would be prime numbers larger than 33550336.
    If they want to store them in a list, then then they wouldn't be asking how to improve their alrogithm which uses longs, would they? It wouldn't really work to do comparisons as a long in hopes to improve on the speed of something which must be stored in a list due to it not fitting in a long, would it? Rhetorical.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Rashakil Fol
    Or you could just write:

    Code:
    if (max >= 6) {
        puts("6");
        if (max >= 28) {
            puts("28");
            if (max >= 496) {
                puts("496");
                if (max >= 8128) {
                    puts("8128");
                    if (max >= 33550336) {
                        puts("33550336");
                    }
                }
            }
        }
    }
    any better one? Cheating this bad is bad

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Cheating this bad is bad
    Cheating is good when not cheating means you write an inefficient algorithm to calculate something that you could more easily look up in a table.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by moonlord
    anyway, erathostene wouldn't work for big numbers. you need an array of trunc(sqrt(x)) elements that can store atleast trunc(sqrt(x)), where x is the number to be tested.
    Sieve of Erastothenes works for large values. The challenges with finding large primes (or large perfect numbers) is representing integers that are greater than can be stored in basic types in C. And, no, it is not necessary to have an array of the order of sqrt(x) where x is the number being tested. All that is needed is to store the prime values themselves .... and the number of actual prime values <= x is substantially less than sqrt(x) for large x.

  13. #13
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    Quote Originally Posted by grumpy
    Sieve of Erastothenes works for large values. The challenges with finding large primes (or large perfect numbers) is representing integers that are greater than can be stored in basic types in C. And, no, it is not necessary to have an array of the order of sqrt(x) where x is the number being tested. All that is needed is to store the prime values themselves .... and the number of actual prime values <= x is substantially less than sqrt(x) for large x.
    right, i was mistaken. and what's the way of storage you think it's best? a list?
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by moonlord
    right, i was mistaken. and what's the way of storage you think it's best? a list?
    There are lots of possible ways (eg a list, an array that you dynamically resize, etc) of doing it. All ways involve some for of tradeoff between speed, memory usage, and a raft of other concerns.

    The definition of "best" is highly subjective as it depends on which concerns are more important to you.

  15. #15
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    well, i've remembered the math teacher told us smth about perfect numbers and checked my notebook:

    Euler has proven that:
    x = 2^n*(2^(n+1)-1) (natural n>0) and 2^(n+1)-1 is prime (Mersenne prime in fact) <=> x is a perfect number

    hope this helps.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  3. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  4. Perfect Numbers - Need Help
    By cstraw in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2001, 05:28 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM