Today's challenge will require a bit of mathematical prowess. Those who were fans of the Project Euler challenges might have some fun with this one...
For further actions, you may consider blocking this person and/or reporting abuse
Woo 3 days in a row!
I'm not too good when it comes to maths and I'm sure there are loads of more efficient algorithms for finding prime factors, but I think this works!
Output:
Turns out there's actually a built in method for this ... So just the formatting I guess...
C++
Output:
Here is my Rust version! These are getting long including the tests, so I might have to find a better way to post em here! Maybe I'll start posting embeds to their Github repo 🤔 (But I don't know if I can embed a single file from a repo...)
Anyways here is today's!
I took a optimization, by never actually calculating the full factorial. Instead I do the prime decomposition, on each of the factorial values (2..n) and append those lists of prime numbers together. This list will be the same as a prime decomposition of the full factorial.
From here I count the occurrences of each of the prime numbers and the format a string matching the examples!
Here is a FORTRAN 90 answer (the module and an example program):
The result looks like:
12! = 479001600 = 2^ 6 + 3^ 4 + 5^ 2 + 7^ 1 + 11^ 1
Note: I hard coded n=12 but I could made n be read from the command line
I can use any language right? How about MATLAB ;)
But here's a haskell solution just for fun:
Perl solution, not using any math libraries:
For 22, it prints 2¹⁹ * 3⁹ * 5⁴ * 7³ * 11² * 13 * 17 * 19.
JavaScript
Not perfect, but it seems to work now. The previous answer I deleted only calculated for the number itself, and not the factorial.
And here is a demo on CodePen.
Python
Here is my simple solution to finish this problem with PHP:
Haskell
The
decomp
function creates a(factor, power)
map describing the decomposition of an integer. ThedecompFactorial
function creates this decomposition for all integers from2
ton
, then merges the maps summing the values for each key. The result is then pretty printed.My solution in Swift, factorial numbers are too big for
Int
but it's really bored to deal withNSDecimalNumber
:I hardcoded prime numbers but here it is
I’m learning Erlang.
I’m not really happy with my solution yet, I feel it could be simpler. I’d rather keep functions that do one thing, though, and avoid
decompose_factorial_into_factors_and_group(X, Y, Z, [])
or something. Hence I didn’t save on the single-argument versions, or combine unrelated functions.Try with:
Too bad @stevemoon didn’t do this one, I would have liked to compare. :-)
Clojure:
Solution
Test
Result
None of these examples work with Google's public key...
taking too long.
Any way to speed up?
\s
How are you developing it? Maybe we can take a look?