10-30-2020 04:09 PM
Here is how I roll:
Likely homework so explanations are left as an exercise for the reader...
10-30-2020 06:22 PM
Of course one quickly runs out of digits and the entire thing could actually be implemented as a lookup table for all useful inputs.
A more interesting solution would be something that can produce results with thousands of significant digits. 😉 (look here for some ideas )
10-30-2020 06:30 PM
Ok, that's the end of the line - slightly faster than I - needed too much time to think about the odd product 😮
10-31-2020 02:41 AM
@altenbach wrote:
Of course one quickly runs out of digits and the entire thing could actually be implemented as a lookup table for all useful inputs.
A more interesting solution would be something that can produce results with thousands of significant digits. 😉 (look here for some ideas )
In this case I would go for something like phython with the mpmath library.
10-31-2020 02:54 AM - edited 10-31-2020 02:56 AM
For N=0 the product is 1. Thank you all for the solutions. I will check them all to understand them and to see which fits better.
10-31-2020 03:05 AM - edited 10-31-2020 03:06 AM
0 is an even number, so this is irrelevant for the problem. However if you want a product result of 1 if N=0, that can easily be implemented. Is zero even allowed as input for your problem. Hard to tell unless we know all the requirements.
10-31-2020 03:07 AM
If 0 is an even number the sum should be 0 and the product also 0.
10-31-2020 03:48 AM
@SebiC98 wrote:
For N=0 the product is 1. Thank you all for the solutions. I will check them all to understand them and to see which fits better.
Here's the tiny change to fix this. See if you understand why. 😉
(The product of an empty array is of course also 1, so some of the conditional indexing solutions will probably work too)
(I also went to U64, but overflow still occurs if the numbers are getting a bit larger, of course)
10-31-2020 03:52 AM
Thank you verry much. Have a great day!
10-31-2020 03:52 AM
@SebiC98 wrote:
If 0 is an even number the sum should be 0 and the product also 0.
No. As I said 0 factorial is 1 and the product of an empty array is also 1, so the correct result for N=0 should probably be one, because the list of odd number is an empty array. See also my other answer.