Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Update MATL Online URL
Source Link
Suever
  • 11.2k
  • 1
  • 24
  • 52

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}

Try it online!

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/

Try it on MATL Online Try it on MATL Online

MATL, 13 bytes

:tptb/sht&Zd/

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}

Try it online!

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/

Try it on MATL Online

MATL, 13 bytes

:tptb/sht&Zd/

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}

Try it online!

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/

Try it on MATL Online

added 40 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33

MATL, 13 bytes

:tptb/sht&Zd/

Try it on MATL Online

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/

Try it on MATL Online

(削除) 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}

Try it online!

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/

Try it on MATL Online

MATL, 13 bytes

:tptb/sht&Zd/

Try it on MATL Online

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/

Try it on MATL Online

(削除) 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}

Try it online!

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/

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}

Try it online!

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/

Try it on MATL Online

added 30 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33

MATL, 13 bytes

:tptb/sht&Zd/

Try it on MATL Online

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/

Try it on MATL Online

(削除) 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}

Try it online!

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/

Try it on MATL Online

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/

Try it on MATL Online

(削除) 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/

Try it on MATL Online

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/

Try it on MATL Online

(削除) 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}

Try it online!

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.

better int version
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
deleted 15 characters in body; deleted 5 characters in body; added 12 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
added 502 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
deleted 501 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
added 318 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
add explanation
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
change primary code
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
add explanation
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
added 266 characters in body
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading
Source Link
Sundar R
  • 6.6k
  • 21
  • 33
Loading

AltStyle によって変換されたページ (->オリジナル) /