Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
3 added 639 characters in body
source | link

C++17 (gcc)C++17 (gcc), 108 bytes

Only use integer arithmetic:

#import<random>
int f(int x,long&n,long&d){n=0;d=1;int
a;while(n=n*x+d,d*=x,a=std::gcd(n,d),n/=a,d/=a,--x);}

Try it online!


C++17 (gcc), 108 bytes

#import<random>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::gcd(n,d);d/=a;}

Try it online!

Same as below, but use C++17's std::gcd.


C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.

C++17 (gcc), 108 bytes

#import<random>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::gcd(n,d);d/=a;}

Try it online!

Same as below, but use C++17's std::gcd.


C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.

C++17 (gcc), 108 bytes

Only use integer arithmetic:

#import<random>
int f(int x,long&n,long&d){n=0;d=1;int
a;while(n=n*x+d,d*=x,a=std::gcd(n,d),n/=a,d/=a,--x);}

Try it online!


C++17 (gcc), 108 bytes

#import<random>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::gcd(n,d);d/=a;}

Try it online!

Same as below, but use C++17's std::gcd.


C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.
2 added 711 characters in body
source | link

C++17 (gcc), 108 bytes

#import<random>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::gcd(n,d);d/=a;}

Try it online!

Same as below, but use C++17's std::gcd.


C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.

C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.

C++17 (gcc), 108 bytes

#import<random>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::gcd(n,d);d/=a;}

Try it online!

Same as below, but use C++17's std::gcd.


C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.
1
source | link

C++ (gcc), 109 bytes

#import<regex>
int f(long&n){double a=0;long
d=1;while(d*=n,a+=1./n,--n);n=a*d+.5;n/=a=std::__gcd(n,d);d/=a;}

Try it online!

Because C++ doesn't have native bigint support, this will definitely overflow for n>20.

Require:

  • gcc's deprecated import statement.
  • gcc's std::__gcd.
  • -O0 (I think so) otherwise the compiler will optimize out d/=a.
  • At least 64-bit long.

Explanation:

  • Let \$d=n!, a=H_n\$.
  • Round a*d to nearest integer by casting a*d+.5 to long, and assign to n. Now n/d is the output.
  • Simplify the fraction with std::__gcd.