MATL, 13 bytes
:tptb/sht&Zd/
Try it on MATL Online Try it on MATL Online
Same method as used in @Dennis' Jelly answer.
:t % Range from 1 to n, duplicate.
pt % Take the product of that (= factorial), duplicate that too.
b/ % Bring the range to top of stack, divide factorial by each element
sh % Sum those. Concatenate factorial and this into a single array.
t&Zd/ % Compute GCD of those and divide the concatenated array elements by the GCD.
(Implicit output, prints denominator first and then the numerator.)
(削除) Floating point inaccuracies mean this doesn't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Part of the code is based on @Chas Brown's Python answer.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:t % Range from 1 to n, duplicate.
pt % Take the product of that (= factorial), duplicate that too.
b/ % Bring the range to top of stack, divide factorial by each element
sh % Sum those. Concatenate factorial and this into a single array.
t&Zd/ % Compute GCD of those and divide the concatenated array elements by the GCD.
(Implicit output, prints denominator first and then the numerator.)
(削除) Floating point inaccuracies mean this doesn't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Part of the code is based on @Chas Brown's Python answer.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:t % Range from 1 to n, duplicate.
pt % Take the product of that (= factorial), duplicate that too.
b/ % Bring the range to top of stack, divide factorial by each element
sh % Sum those. Concatenate factorial and this into a single array.
t&Zd/ % Compute GCD of those and divide the concatenated array elements by the GCD.
(Implicit output, prints denominator first and then the numerator.)
(削除) Floating point inaccuracies mean this doesn't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Part of the code is based on @Chas Brown's Python answer.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:tpt - Range from 1 to n, duplicate. Take the product of that (= factorial), duplicate that too.
b/ - Bring the range to top of stack, divide factorial by each element
sh - Sum those. Concatenate factorial and this into a single array.
t&Zd/ - Compute GCD of those and divide the concatenated array elements by the GCD.
:t % Range from 1 to n, duplicate.
pt % Take the product of that (= factorial), duplicate that too.
b/ % Bring the range to top of stack, divide factorial by each element
sh % Sum those. Concatenate factorial and this into a single array.
t&Zd/ % Compute GCD of those and divide the concatenated array elements by the GCD.
Implicit(Implicit output, prints denominator first and then the numerator.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
(削除) Floating point inaccuracies mean these don'tthis doesn't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was probably a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. CodePart of the code is partly based on @Chas Brown's Python answer.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:tpt - Range from 1 to n, duplicate. Take the product of that (= factorial), duplicate that too.
b/ - Bring the range to top of stack, divide factorial by each element
sh - Sum those. Concatenate factorial and this into a single array.
t&Zd/ - Compute GCD of those and divide the concatenated array elements by the GCD.
Implicit output, prints denominator first and then the numerator.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
(削除) Floating point inaccuracies mean these don't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was probably a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Code is partly based on @Chas Brown's Python answer.
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:t % Range from 1 to n, duplicate.
pt % Take the product of that (= factorial), duplicate that too.
b/ % Bring the range to top of stack, divide factorial by each element
sh % Sum those. Concatenate factorial and this into a single array.
t&Zd/ % Compute GCD of those and divide the concatenated array elements by the GCD.
(Implicit output, prints denominator first and then the numerator.)
(削除) Floating point inaccuracies mean this doesn't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Part of the code is based on @Chas Brown's Python answer.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:tpt - Range from 1 to n, duplicate. Take the product of that (= factorial), duplicate that too.
b/ - Bring the range to top of stack, divide factorial by each element
sh - Sum those. Concatenate factorial and this into a single array.
t&Zd/ - Compute GCD of those and divide the concatenated array elements by the GCD.
Implicit output, prints denominator first and then the numerator.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
(削除) Floating point inaccuracies mean these don't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was probably a typo, this returns the same answer as the other answers for n=20.
Here's Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Code is partly based on @Chas Brown's Python answer.
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:tpt - Range from 1 to n, duplicate. Take the product of that (= factorial), duplicate that too.
b/ - Bring the range to top of stack, divide factorial by each element
sh - Sum those. Concatenate factorial and this into a single array.
t&Zd/ - Compute GCD of those and divide the concatenated array elements by the GCD.
Implicit output, prints denominator first and then the numerator.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
(削除) Floating point inaccuracies mean these don't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was probably a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). This increases the input range to allow n upto 43. Code is partly based on @Chas Brown's Python answer.
MATL, 13 bytes
:tptb/sht&Zd/
Same method as used in @Dennis' Jelly answer.
:tpt - Range from 1 to n, duplicate. Take the product of that (= factorial), duplicate that too.
b/ - Bring the range to top of stack, divide factorial by each element
sh - Sum those. Concatenate factorial and this into a single array.
t&Zd/ - Compute GCD of those and divide the concatenated array elements by the GCD.
Implicit output, prints denominator first and then the numerator.
Alternate (original) method using LCM instead of factorial:
(削除) 16 (削除ここまで) 15 bytes
:t&Zmtb/sht&Zd/
(削除) Floating point inaccuracies mean these don't work for n = 20, because the intermediate values are too large. (削除ここまで) Looks like the test case output was probably a typo, this returns the same answer as the other answers for n=20.
Here's an integer type-preserving version (25 bytes) I tried in the meantime, before finding this out:
25 bytes, input upto 43
O1i:3Y%"t@*b@*b+wht&Zd/Z}
Casts the numbers to uint64 before operating on them, does the arithmetic explicitly in a loop (without using prod or sum). More importantly, divides the partial numerators and denominators by their GCD every step along the way, at the end of each iteration. This increases the input range to allow n upto 43. Code is partly based on @Chas Brown's Python answer.
- 6.6k
- 21
- 33