Okay folks, this should be simple, but maybe not.

The goal is to write a function that takes a positive integer and returns a list of its prime factors. So if you did 12 you should get the list [2, 2, 3]

As neither 1 nor zero are prime, as a result should return an empty list.

This is taken from a , so if you have not done this one I encourage you to do so. If you are not into then solve it however you like.

@Absinthe Easy to solve yes, but rather difficult to solve efficiently!

@freemo @Absinthe An even deeper rabbit hole with TDD: "You want to test your factorization? Well, you need a primality test... But wait! Who's gonna test your primality test? Well, you need a prime number generator... But wait!..."
No monkeys, no coconuts, no bottles, no fun...

Follow

@namark

Thats solvable (goes into NP stuff but I wont invoke this here)...

Think of it like this creating prime numbers is a much easier task than checking if a number is prime. Its the whole reason cryptography works, we can generate large primes for our keys but getting those primes out after the fact is computationally difficult.

So a TDD would be a simple matter of generating a set of primes (easy) then multiply them together to get the value under test. Then send that value off to be tested and verify if the results agree with the original list of primes you generated.

Reversing the process makes all the difference in the world when testing stuff like this.

@Absinthe

@freemo

Checking for primality is not that hard either, the factorization is hard and is the reason cryptography works (certain schemes).

That said, even if it's an easier task computationally, it's not necessarily easier to implement, especially efficiently, especially for large numbers.

Sure, skip the primality test, if your genrerator does not rely on it, but who's going to test the generator?
Maybe a trivial primality test that does not need a test itself? (oh no already violating TDD) Use it to test the generator, which you can then use to test a more sophisticated primality test that you can then use to test the generator for even bigger numbers? This is a very trollish kata for TDD, and an example of a problem where you're better off relying on theory to prove correctness, an maybe only testing some components not the problem/solution as a whole.

@Absinthe

@namark

Yea sorry I misspoke, I should have said factorization. Sieves would make it easy to chec if something is prime.

@Absinthe

@freemo @namark one of the fun things about TDD is how it can organically create an algorithm that you might not do just by sitting down and hacking at it.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.