from math import*
Missing space?
for i in range(1, integerSqrt - 1):
if n % i == 0:
nDivisors += 2
I'm not saying it's better here, but you can achieve a lot with list comprehensions. For example, 2 * len([i for i in range(1, integerSqrt -1) if n % i == 0]
would be equivalent. And less readable, so don't use it here!
incrementTriangle += 1
triangleNumber = triangleNumber + incrementTriangle
Be consistent with increments (triangleCount += incrementTriangle
)
elif divisors >= 500:
print "The first triangle number w/ 500+ divisors is", triangleNumber
return 0
Using returns or breaks in a loop is usually a bad idea if it can be avoided. It means that you need to scan the whole loop to see where the loop could end earlier than expected. In this case, what's preventing you from doing while divisors < 500
. As an added bonus, you would stop mixing output and actual logic.
print "The first triangle number w/ 500+ divisors is", triangleNumber
Use the print()
function provided in Python 2.6. It will make your code Python 3 compatible and.. more pythonic. If you want more flexibly, also consider using format strings. This would give:
print("The first triangle number w/ 500+ divisors is %d" % triangleNumber)
So, you have a main function and call it like this:
main()
You should instead use the __main__
__main__
idiom, as it will allow your file to be imported (for example to use divisorsCount).
And last, but not least, you should really follow the PEP 8, as others said in the comments. Consistency is really important to make your code more readable!
from math import*
Missing space?
for i in range(1, integerSqrt - 1):
if n % i == 0:
nDivisors += 2
I'm not saying it's better here, but you can achieve a lot with list comprehensions. For example, 2 * len([i for i in range(1, integerSqrt -1) if n % i == 0]
would be equivalent. And less readable, so don't use it here!
incrementTriangle += 1
triangleNumber = triangleNumber + incrementTriangle
Be consistent with increments (triangleCount += incrementTriangle
)
elif divisors >= 500:
print "The first triangle number w/ 500+ divisors is", triangleNumber
return 0
Using returns or breaks in a loop is usually a bad idea if it can be avoided. It means that you need to scan the whole loop to see where the loop could end earlier than expected. In this case, what's preventing you from doing while divisors < 500
. As an added bonus, you would stop mixing output and actual logic.
print "The first triangle number w/ 500+ divisors is", triangleNumber
Use the print()
function provided in Python 2.6. It will make your code Python 3 compatible and.. more pythonic. If you want more flexibly, also consider using format strings. This would give:
print("The first triangle number w/ 500+ divisors is %d" % triangleNumber)
So, you have a main function and call it like this:
main()
You should instead use the __main__
idiom, as it will allow your file to be imported (for example to use divisorsCount).
And last, but not least, you should really follow the PEP 8, as others said in the comments. Consistency is really important to make your code more readable!
from math import*
Missing space?
for i in range(1, integerSqrt - 1):
if n % i == 0:
nDivisors += 2
I'm not saying it's better here, but you can achieve a lot with list comprehensions. For example, 2 * len([i for i in range(1, integerSqrt -1) if n % i == 0]
would be equivalent. And less readable, so don't use it here!
incrementTriangle += 1
triangleNumber = triangleNumber + incrementTriangle
Be consistent with increments (triangleCount += incrementTriangle
)
elif divisors >= 500:
print "The first triangle number w/ 500+ divisors is", triangleNumber
return 0
Using returns or breaks in a loop is usually a bad idea if it can be avoided. It means that you need to scan the whole loop to see where the loop could end earlier than expected. In this case, what's preventing you from doing while divisors < 500
. As an added bonus, you would stop mixing output and actual logic.
print "The first triangle number w/ 500+ divisors is", triangleNumber
Use the print()
function provided in Python 2.6. It will make your code Python 3 compatible and.. more pythonic. If you want more flexibly, also consider using format strings. This would give:
print("The first triangle number w/ 500+ divisors is %d" % triangleNumber)
So, you have a main function and call it like this:
main()
You should instead use the __main__
idiom, as it will allow your file to be imported (for example to use divisorsCount).
And last, but not least, you should really follow the PEP 8, as others said in the comments. Consistency is really important to make your code more readable!
from math import*
Missing space?
for i in range(1, integerSqrt - 1):
if n % i == 0:
nDivisors += 2
I'm not saying it's better here, but you can achieve a lot with list comprehensions. For example, 2 * len([i for i in range(1, integerSqrt -1) if n % i == 0]
would be equivalent. And less readable, so don't use it here!
incrementTriangle += 1
triangleNumber = triangleNumber + incrementTriangle
Be consistent with increments (triangleCount += incrementTriangle
)
elif divisors >= 500:
print "The first triangle number w/ 500+ divisors is", triangleNumber
return 0
Using returns or breaks in a loop is usually a bad idea if it can be avoided. It means that you need to scan the whole loop to see where the loop could end earlier than expected. In this case, what's preventing you from doing while divisors < 500
. As an added bonus, you would stop mixing output and actual logic.
print "The first triangle number w/ 500+ divisors is", triangleNumber
Use the print()
function provided in Python 2.6. It will make your code Python 3 compatible and.. more pythonic. If you want more flexibly, also consider using format strings. This would give:
print("The first triangle number w/ 500+ divisors is %d" % triangleNumber)
So, you have a main function and call it like this:
main()
You should instead use the __main__
idiom, as it will allow your file to be imported (for example to use divisorsCount).
And last, but not least, you should really follow the PEP 8, as others said in the comments. Consistency is really important to make your code more readable!