const VecStr fastPermuteVectorOneLength(std::string vec) { VecStr ans; int numOfPermutes = Factorial((int)vec.size()); ans.reserve(numOfPermutes); do { ans.push_back(vec); } while (std::next_permutation(vec.begin(), vec.end())); return ans; }
int main() { for (int i = 1; i <= 10; ++i) std::cout << i << "!=" << Factorial(i) << std::endl; /*std::cout << "Enter number: "; std::cin >> n; std::cout << "Result is "; std::cout << Factorial(n) << std::endl;*/ return 0; }
void Factorial(long n) { if (n == 0) { res = 1; } else { Factorial(n-1); res = n * res; } }
// Alright this is it -- // Let's think about the "definition" of factorial for a second. // We said 3! = 1 * 2 * 3, which is equivalent to 3 * 2!. We know 1! = 1, so by using // those simple facts we design our "factorial function" to mimic these properties. uint Factorial(uint num) { // Factorial of one is one, thus we'll return one if(num == 1) return 1; else return num * Factorial(num - 1); // Otherwise it's num * the factorial of (num - 1) }
double msin(double x,double e) { double j,a,b,k; b= x ; j = 1 ; do { k = power(-1,j); a =k*power(x,2*j+1)/Factorial(2*j+1); b =b + a; j+=1; } while(a*k>e); return b; }
double mex(double x,double e){ double j,a,b; b= 1; j = 1 ; do { a =power(x,j)/Factorial(j);b = b + a; j+=1; } while(a>e); return b; }
bool TestFactorial( ) { bool ok = true; cout << "Testing Factorial" << endl; TESTCHECK( Factorial( 12 ), 479001600, &ok ); TESTCHECK( Factorial( 20LL ), 2432902008176640000LL, &ok ); TESTCHECK( Factorial( 20. ), 2432902008176640000., &ok ); TESTCHECKF( Factorial( 50. ), 3.0414093203509456e64, &ok ); TESTCHECKFE( Factorial( 100. ), 9.3326e157, &ok, 1.e-5 ); TESTCHECK( BinomialCoefficient( 19, 7 ), 50388., &ok ); TESTCHECK( BinomialCoefficient( 20, 10 ), 184756., &ok ); if ( ok ) cout << "Factorial PASSED." << endl << endl; else cout << "Factorial FAILED." << endl << endl; return ok; }
/** Computes a factorial. */ static INT Factorial(INT A) { if(A == 0) { return 1; } else { return A * Factorial(A - 1); } }
/** Computes a factorial. */ static int32 Factorial(int32 A) { if(A == 0) { return 1; } else { return A * Factorial(A - 1); } }
Polynom Polynom::TimeShift(double shift) const { vector<double> v; Polynom p(*this); for (int i = 0; i <= Power(); ++i) { v.push_back(p(shift) / double(Factorial(i))); p = p.Differenciate(); } return Polynom(v); }
void /* Partition(int N, double Rtheta, double IJ[N*2]) */ Partition(int N, double Rtheta, double IJ[]) { int level, var; /* double I[STRLEN], J[STRLEN], factor, sum;*/ double factor, sum; for (var = 1; var <= N; var++) { sum = 0.0; /* first term in hierarchy, occurs (N-1)! times */ level = 1; sum += (Factorial(N - 1) * (d[level].m[var].X - Rtheta)); /* last term in hierarchy, also occurs (N-1)! times */ level = N; sum += (Factorial(N - 1) * ReturnDiff(level, var)); for (level = 2; level < N; level++) { /* each difference occurs (j - 1)!(N - j)! times */ factor = 1.0 * Factorial(level - 1) * Factorial(N - level); sum += factor * ReturnDiff(level, var); } /* for level ... IJ[0][var-1] = I[var] = sum / Factorial(N); IJ[2][var-1] = J[var] = (d[1].m[var].X - Rtheta - I[var]); */ IJ[var - 1] = sum / Factorial(N); IJ[N + var - 1] = (d[1].m[var].X - Rtheta - IJ[var - 1]); } /* for var ... */ /* Rprintf("\nvar\tI\tJ\tTot\n"); for (var = 1; var <= N; var++) { Rprintf("Var%d \t%4.3lf\t%4.3lf\t%4.3lf\n", var, I[var], J[var], I[var] + J[var]); }*/ } /* Partition() */
/* * Compute and return the factorial of a value, using a recursive algorithm. Zero factorial * will return 1. * @param value an unsigned integer containing the value for which the factorial will be computed * @return an unsigned integer containing the computed factorial of the value */ unsigned int Factorial(unsigned int value) { if (value == 1 || value == 0) { return 1; } else { return (value * Factorial(value-1)); } }
void GeneratePermutedList( int *list, int listLength, int permute ) { for( int i = 0 ; i < listLength ; i++ ) { list[i] = i; } // we can't calculate > 12 factorial, so we can't easily build a permuted list if( listLength > 12 ) { return; } // calculate listLength factorial int maxPermute = Factorial( listLength ); // recursively permute PermuteList_r( list, listLength, permute, maxPermute ); }
int Factorial(int n){ if(n == 0){ return 1; } else { return(n*Factorial(n-1)); } return 0; }
void test60Fact() { std::chrono::time_point<std::chrono::system_clock> start, end; std::chrono::duration<double> elapsed_time; start = std::chrono::system_clock::now(); BigInt fact60 = Factorial(60); end = std::chrono::system_clock::now(); elapsed_time = end - start; #ifdef _PRINT_VALS std::cout<< "fact60 took: " << elapsed_time.count() << " computing " << fact60 << std::endl; #endif std::vector<limb_t> actual{18396530,1065373609,393476149,835766904,1949166040,1155626993,1901353954,234881024,0}; std::cout << "60! Correct? " << testEquals(fact60, actual) << std::endl; }
// Tests factorial of negative numbers. TEST(FactorialTest, Negative) { // This test is named "Negative", and belongs to the "FactorialTest" // test case. EXPECT_EQ(1, Factorial(-5)); EXPECT_EQ(1, Factorial(-1)); EXPECT_GT(Factorial(-10), 0); // <TechnicalDetails> // // EXPECT_EQ(expected, actual) is the same as // // EXPECT_TRUE((expected) == (actual)) // // except that it will print both the expected value and the actual // value when the assertion fails. This is very helpful for // debugging. Therefore in this case EXPECT_EQ is preferred. // // On the other hand, EXPECT_TRUE accepts any Boolean expression, // and is thus more general. // // </TechnicalDetails> }
int main() { int i; int a[] = {1,2,3,4,5,6,7,8,9}; printf("Reverse to display an array:\n"); Reverse(a, 8); i = HCF(1470, 693); printf("Factorial 5! = %d\n", Factorial(5)); printf("Greates common devisor 1470 and 693 %d\n", i); int n; n = 3, i; printf("%d\n", n); return 0; }
void main() { int f1 = 1, f2 = 1, n; printf("%d\n", Square(2, 6)); printf("%d\n", Factorial(5)); printf("피보나치 수열의 n항 입력: "); scanf("%d", &n); printf("피보나치 %d항은 %d\n", n, Fibonacci(f2, f1, n)); }
void test20FactQuad() { std::chrono::time_point<std::chrono::system_clock> start, end; std::chrono::duration<double> elapsed_time; start = std::chrono::system_clock::now(); BigInt fact = Factorial(20); BigInt factSquared = fact * fact; factSquared *= factSquared; end = std::chrono::system_clock::now(); elapsed_time = end - start; #ifdef _PRINT_VALS std::cout<< "fact20quad took: " << elapsed_time.count() << " computing " << factSquared << std::endl; #endif std::vector<limb_t> actual{166337208,764515097,443847783,29956932,1554513582,703611904,0,0}; std::cout << "(20!)^4 Correct? " << testEquals(factSquared, actual) << std::endl; }
void TestPermutations( void ) { int list[SOUND_MAX_LIST_WAVS]; for( int len = 1 ; len < 5 ; len++ ) { common->Printf( "list length: %i\n", len ); int max = Factorial( len ); for( int j = 0 ; j < max * 2 ; j++ ) { GeneratePermutedList( list, len, j ); common->Printf( "%4i : ", j ); for( int k = 0 ; k < len ; k++ ) { common->Printf( "%i", list[k] ); } common->Printf( "\n" ); } } }
int BernsteinPoly::Factorial(int num, int result) { if(num == 0) return 1; else if( num == 1) return result; else { num--; return Factorial( num, result*num ); } }
int main() { int num = Factorial(14); printf("Factorial = %d\n",num); printf("Method 1:Number of Zero = %d\n", GetZeroNumber1(num)); printf("Method 2:Number of Zero = %d\n",GetZeroNumber2(25)); printf("Method 3:Number of Zero = %d\n",GetZeroNumber3(25)); printf("Method 1 offset = %d\n",GetOneBitOffset(25)); printf("Method 2 offset = %d\n",GetLowestOneBit(25)); getch(); return 0; }
int main(){ //inicio de ejecución! long int x; //variables para operar el menú principal while(true){ //nos permite poder seguir mostrando el menu principal //TODO: buscar como mostrarlo más bonito (limpiar pantalla siempre en esta posición) printf("\nCalcula el factorial de (entero positivo)... >>> "); //muestra prompt para que el usuario entre datos scanf("%d", &x); //captura un entero del teclado y lo asigna a x if(x < 0) //validación de datos printf("\n\tDato no valido!\n"); //mensaje relacionado a la validación de datos else printf("\n\t%d! = %d \n", x, Factorial(x)); //si el dato es adecuado muestra su factorial } return 0; }
void SlidingTilePuzzle::GetRankFromState(const SlidingTilePuzzleState& state, uint64_t& rank) { int size = state.width*state.height; rank = 0; int s = 0; for (int i = 0; i < size; i++) { s = 0; for (int j = i + 1; j < size; j++) if (state.puzzle[j] < state.puzzle[i]) s++; rank += s*Factorial(size - 1 - i); } }
int main(void) { int n; int x; printf("Enter n: "); scanf("%i", &n); printf("Enter x: "); scanf("%i", &x); double sum = 1; for (int i = 1; i <= n; i++) { sum = sum + Factorial(i) / pow(x, i); } printf("Sum is : %.5lf\n", sum); return 0; }
unsigned int Factorial(unsigned int value) { // If greater than 1 // Call Factorial() until value is 1 while(value >= 1) { if(value == 1) { return value; } else { return (value * Factorial(value - 1)); } } // Else, return 1; return 1; }
int main(){ Factorial(); int n; while (scanf("%d", &n) != EOF) { printf("%d!\n", n); int i; for (i = M - 1; !factorial[n][i]; i--); for (; i >= 0; i--) printf("%d", factorial[n][i]); puts(""); } return 0; }
void TestFactorial() { uint c; uint checkbuff[14]; //const uint * const here = checkbuff; Hash H = NewHash(FactHasher); for (c=0; c<TESTFACTORIALMAX; c++) { checkbuff[c] = Factorial(H, c); //printf("%d\n", Factorial(H, c)); } //asm("int3"); if (TestCheckBufferWithFunction(checkbuff, TESTFACTORIALMAX, FactCheck) == false) printf ("test failed.\n"); DelHash(H); printf("Factorial, using a hash table for memoization passed.\n"); }
static uint Factorial(Hash const H, const uint n) { int ans; if (n==0) return 1; else { ans = LookupAnswer(H, n); if (!ans) { ans = n * Factorial(H, n-1); AddAnswer(H, n, ans); } //else printf("found %d in the hashtable!\n", ans); return ans; } return 0; }
void main() { a = 16807; m = 127; m = m * 256 + 255; m = m * 256 + 255; m = m * 256 + 255; q = m / a; r = m % a; Factorial(7); WriteLong(res); WriteLine(); WriteLine(); FibRec(11); WriteLong(res); WriteLine(); WriteLine(); Hanoi(3); WriteLine(); }