Boredom creates programs!
Aug 15
This is what happens when I’m bored – a mate of mine messaged me for a tip on how to write a program for his homework at uni – I ended up writing the whole program as a test for myself in Python.
#!/usr/bin/python
#Craig: Write and test a method that returns the sum of all factors of a given positive integer n, where the factors include 1 but exclude n itself. For example, the sum of the factors of 12 is 16 (1+2+3+4+6 =
16).
#yaleman:
#first you have to get the factors
#iterative modulo would be the easiest
number_to_factor = 12
sum_of_numbers = 0
debug = 0
for i in range(1,(number_to_factor – 1 )):
test = number_to_factor % i
if debug == 1:
print "i "+`i`
print "test "+`test`
if test == 0:
sum_of_numbers += i
print "Sum of factors of "+`number_to_factor`+" is "+`sum_of_numbers`
I think I might have to start looking at some of the bounties for stuff in the open source community – I know how to do some of it, and if I don’t, I can learn!
Facebook
RSS