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);}
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;}
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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis 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;}
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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis 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);}
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;}
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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis 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;}
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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis 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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis 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;}
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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis 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;}
Because C++ doesn't have native bigint support, this will definitely overflow for n>20.
Require:
- gcc's deprecated
importstatement. - gcc's
std::__gcd. -O0(I think so) otherwise the compiler will optimize outd/=a.- At least 64-bit
long.
Explanation:
- Let \$d=n!, a=H_n\$.
- Round
a*dto nearest integer by castinga*d+.5tolong, and assign ton. Nown/dis the output. - Simplify the fraction with
std::__gcd.