Was testing your function and noticed that it gave me a bad access error, probably due to the recursion depth, when calculating factors(100_003). That said, I appreciate the way you thought of this recursively, very smart.
I agree with antong01. This problem is best solved in O(n) by not sorting either array, but instead making use of a dictionary. Most of the solutions submitted either use sorting which results in O(n log n) or searching through an array, which results in O(n^2) overall. While simple and concise to write, they don't scale as well.
Was testing your function and noticed that it gave me a bad access error, probably due to the recursion depth, when calculating
factors(100_003)
. That said, I appreciate the way you thought of this recursively, very smart.This comment is hidden because it contains spoiler information about the solution
I agree with antong01. This problem is best solved in
O(n)
by not sorting either array, but instead making use of a dictionary. Most of the solutions submitted either use sorting which results inO(n log n)
or searching through an array, which results inO(n^2)
overall. While simple and concise to write, they don't scale as well.Please mark your posts as having spoiler content even in Solutions when they have spoilers.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
I suppose I should have incremented i in the else clause of my guard statement, as right now given an invalid character it would infinite loop.