Advertisement
lswest

FCM C&C #56

Dec 11th, 2011
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. #!/bin/bash
  2. # This is an example python file for FCM C&C #56, which contains code to calculate abundant numbers within the range [2,500).
  3.  
  4. # Pseudocode (Abundant numbers in Python)
  5. import math
  6.  
  7. # Function based off the fact that any factors of a number are based around the square root.  For each number (i) between 2 and sqrt(n) if the number is a factor, then n/i is the other factor.  Complexity: O(sqrt(n))
  8. def sumfacs(n):
  9.     t=1
  10.     s=int(n**0.5)
  11.     i=2
  12.     while i<=s:
  13.         if n%i==0:
  14.             if n!= i*i: t+=n/i
  15.             t+=i
  16.  
  17.         i+=1
  18.     return int(t)
  19.  
  20. abundant=[]
  21.  
  22. # Since 12 is the smallest abundant number (factors of 12 = 1,2,3,4,6, sum of factors = 16, 16>12)
  23. for i in range(12, 500):
  24.     if sumfacs(i) > i:
  25.         abundant.append(i)
  26.  
  27. print("All abundant numbers between 12 and 499: ",abundant)
  28. print("Total number found: ",len(abundant))
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement