I'm attempting Project Euler problem 484. I'm in the beginning stages of the problem and am just searching for patterns in the numbers.
The problem requests me to find an "arithmetic derivative". For example, to find the arithmetic derivative of 60:
60 = 2^2 * 3^1 * 5^1
60' = (2/2 + 1/3 + 1/5) * 60 = 92
I utilized the built in primefac algorithm, and created a method for the arithmetic derivative. Here is my code:
import primefac
def ad(n):
ans = 0
for i in set(primefac.primefac(n)):
ans += 1.0*list(primefac.primefac(n)).count(i)/i
return n*ans
print ad(30) , 31.0 // PRINTS 31.0 31.0
print int(ad(30)) // PRINTS 30
print ad(30) == 31.0 // PRINTS False
Python casts 31.0 as an int to 30. Is this a floating point error? What is more perplexing is that ad(30) prints 31.0 but returns False when evaluated against each other.
print(repr(ad(30)))display? And what version of Python are you using?primefacisn't "built in", it's an external module.print(more accurately, withfloat.__str__) which I'm fairly sure was addressed somewhere in the 3.0 branch.intwill truncate/round down, so everything you're showing makes sense. If you want to test for "equality" with floats, it's best to use some "closeness" test (e.g. Python3.5+math.isclose) or some similar backport.numpy.isclose()may also be an option. (See also PEP485)