I'm making a small factorial function to assist with some math I have to do for a function.
#include<iostream>
using std::cout;
using std::endl;
class HandsShaking{
private:
unsigned long long fac(int n){
return fac(n, 1);
}
unsigned long long fac(unsigned long long n, unsigned long long ret){
if(n == 1){
return ret;
}else{
return fac(n-1, ret*n);
}
}
public:
unsigned long long countPerfect(unsigned long long n){
unsigned long long m = n/2;
cout<<fac(n)<<endl;
cout<<(fac(m+1)*fac(m))<<endl;
return fac(n)/(fac(m+1)*fac(m));
}
};
I'm using tail recursive and unsigned long longs everywhere so I doubt it is am overflow.
I haven't separated the functions and definitions, but my submission (the count perfect function) specifies one file.
The function seems to work for all numbers up to 22 and fails for all numbers after.