aboutsummaryrefslogtreecommitdiffstats
path: root/12-Highly-Divisible-Triangular-Number/bigfactors2.py
diff options
context:
space:
mode:
Diffstat (limited to '12-Highly-Divisible-Triangular-Number/bigfactors2.py')
-rw-r--r--12-Highly-Divisible-Triangular-Number/bigfactors2.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/12-Highly-Divisible-Triangular-Number/bigfactors2.py b/12-Highly-Divisible-Triangular-Number/bigfactors2.py
new file mode 100644
index 0000000..9e51502
--- /dev/null
+++ b/12-Highly-Divisible-Triangular-Number/bigfactors2.py
@@ -0,0 +1,34 @@
+import PIL
+import math
+
+# Problem 12 Highly divisible triangular number
+# finds the first number with over 500 factors
+
+
+def countfactors(num):
+ factors = 0
+ # we only need to know about the FIRST HALF of factors,
+ # one factor implies a second
+ root = int(math.ceil(math.sqrt(num)))
+ divs = range(1, root)
+ for d in divs:
+ if(num % d == 0):
+ factors += 2
+
+ # Correction if the number is a perfect square
+ if (root * root == num):
+ factors -= 1
+ return factors
+
+
+#### MAIN #####
+i = 1
+k = 1
+j = 0
+while(k < 500):
+ j += i
+ k = countfactors(j)
+ print(str(j) + " has " + str(k) + " factors")
+ i += 1
+
+print("Ding! Ding! {} has over 500 factors, wow!".format(j))