Calculation Problem

Discussion in 'C' started by inspiration, Jun 13, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Example: 28 = 1 + 2 + 4 + 7 + 14 ==> if one fully paid equal to the sum of its divisor is

    my c-code this:

    Code:
    # Include <stdio.h> 
    # Include <conio.h> 
    
    int main () 
    ( 
     long z, i, sum = 0; 
    
     for(z = 1, z <= 1000000; z + +) 
     ( 
      for(i = 1; i <z; i + +) 
      ( 
       if(z% i == 0) sum + = i; 
      ) 
      if(sum == x) printf ("% \ n ld", z); 
      sum = 0; 
     ) 
    
     return 0; 
    )
    now I have the problem that it takes very long (on my athlon 1800 + I got after 5 min terminated because he was only at about 80 000)

    can this be done efficiently?
     
  2. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    for (i = 1; i <z; i + +) 
    if (z% i == 0) sum + = i; 
    looks like for Maich
    Code:
    for (i = 1, i * i <z; i + +) 
    if (z% i == 0) (sum + = i; sum + = z / i;) 
    if (z% i == 0) sum + = i;
     
  3. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    is the last line as aware of?
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    for (z = 1, z <= 1000000; z + +) 
     ( 
      for (i = 1; i <z; i + +) 
      ( 
       if (z% i == 0) sum + = i; 
      ) 
      if (sum == x) printf ("% ld \ n", z); 
      sum = 0; 
     )
    You could be in the "i loop" Check whether the sum is too large and, if yes cancel.
    Code:
    for (i = 1; i <z; i + +) 
      ( 
       if (z% i == 0) 
         (Sum + = i; 
           if (sum> z) = z i / / or break, (I do not break :-)) 
         ) 
      )
    I too have to believe that the i-loop search only to z / 2:
    for (i = 1, (i * 2) <= z; i + +)

    and I would look for from big to small
    for (i = (z +1) / 2; i> 0, - i) / / the z +1 to be sure,
    / / is that even the half of tested

    But then I must terminate with i = 0 to change
     
  5. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    # Include <stdio.h> 
    # Include <math.h> 
    int main () 
    ( 
      long z, i, sum = 1, / / there is always a divisor 
    
      for (z = 1, z <= 1000000; z + +) 
        ( 
          for (i = sqrt (z), i> 1, - i) / / without a splitter to the root 
            (If (z% i == 0) 
                 (Sum + = i; 
                   sum + = z / i / / splitter, each has its "co-parter" 
                   if (sum> z) i = 0; / / demolition as the sum too large 
                 ) 
            ) 
           if (sum == x) printf ("% ld \ n", z); 
           sum = 1, / / there is always a divisor 
        ) 
    
      return 0; 
    )
    NEN on Pentium 450s in about two minutes the whole Mille calculated through
    but only when the figures found

    if I do not know all of the
     
  6. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    That's all. But since you'll have to come up, many time-saving things, so you can find as many. The fifth is 33,550,336, and already the sixth 8589869056 ...
     
  7. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    It may interest one or the other too:

    Perfect numbers are always a sum of consecutive numbers, eg:
    6 = 1 +2 +3,
    28 = 1 +2 +3 +4 +5 +6 +7,
    496 = 1 +2 +3 +4 +5 +...+ 30 +31
    8128 = 1 +2 +3 +4 +5 +...+ 126 +127

    Euclid proved using the geometric series:

    n for some numbers p = 1 +2 +4 +8 +...+ 2 ^ n = 2 ^ (n +1) -1 is a prime number.
    In each such case, 2 ^ n · p is perfect.
    6 = 2 * (2 ² -1)
    28 = 2 ² * (2 ³ -1)

    It was later proved by Euler, that this rule all even, perfect numbers provides - if you find a suitable prime. Therefore, virtually every new large prime Fund is associated with a further big perfect number.

    The question of whether it is also odd perfect numbers, is still unclear. In case of existence of these would need to be larger than 10 ^ 100 and have at least 11 different prime divisors.
     
  8. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Yes.
    Top I add the paired dividers.
    So for 36 I'll do as
    2 18
    3 12
    4 9
    and down again I'll do the 6
    and the one I've forgotten.

    yes, if every perfect number ne triangular number must be, because then you have still NEN fine accelerator.
     
  9. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    f I want to study up to z / 2 why I did then as a termination condition not> or <z / 2 z and square root but i?
     
  10. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    because sqrt (z) is already sufficient.
    z / 2 would be a waste of time.

    dividers for each i with z% i == 0, the opposites meet g = z and z% i% g == 0. so you only need to go to root, as would i == g. Otherwise, is always a small and a larger than the root.
     
  11. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
  12. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    but why?
    As I see, all tags are correct in my post...
    :)
     
  13. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    I did that for you.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice