Пример #1
0
int main()
{
  printf("Project Euler - Problem 34:\n"
         "Find the sum of all numbers which are equal to the sum of the factorial of their digits.\n\n");

  const int maxNum = 1000000;
  int total = 0;
  int number = 0;

  for (number=3 ; number<maxNum ; number++)
  {
      // Calculate the factorials
      int numberSum = 0;
      int decompose = number;
      while (decompose > 0)
      {
          numberSum += getFactorial(decompose % 10);
          decompose /= 10;
      }

      // Compare
      if ( numberSum == number )
          total += numberSum;
    }

      printf("Sum: %d\n", total);

      return 0;
}
Пример #2
0
int main(int argc, char const *argv[])
{
    unsigned int value = atoi(argv[1]);
    printf("%d! = \n", value);
    printf("%u\n", getFactorial(value));
    return 0;
}
Пример #3
0
/*
 *  * Recursive function to find factorial of a number
 *   */
int getFactorial(int N){
    /* Exit condition to break recursion */
    if(N <= 1){
         return 1;
    }
    /*  N! = N*(N-1)*(N-2)*(N-3)*.....*3*2*1  */ 
    return N * getFactorial(N - 1);
}
Пример #4
0
/* Factorial Creator */
int getFactorial(int input) {
	if (input <= 1) {
		return 1;
	} else {
		return (input * getFactorial(input - 1));
	}
	return 0;
}
Пример #5
0
int main(){
    int N, nFactorial, counter;
    printf("Enter a number \n");
    scanf("%d",&N);
 
    printf("Factorial of %d is %d", N, getFactorial(N));
     
    //getch();
    return 0;
}
Пример #6
0
int main() {
	int sequence = 0;
	int fixDigit = 0;
	int usedSequence[10] = { 0, };
	int cnt = 0;

	while (fixDigit != 9) {
		sequence += getFactorial(9 - fixDigit);
		if (sequence >= 1000000) {
			sequence -= getFactorial(9 - fixDigit);
			fixDigit++;
			printf("%d\n", cnt);
			cnt = 0;
		}else cnt++;
	}


	system("pause");
	return 0;
}
Пример #7
0
void Menu::executeFACTORIAL() {
	int nNum = 0;

	cout << "\t팩토리얼 구하기 함수를 선택하셨습니다." << endl;
	cout << "■ 숫자를 입력해주세요 : ";
	cin >> nNum;

	int nFact = getFactorial(nNum);

	cout << "▶ !" << nNum << " : " << nFact << endl;
}
Пример #8
0
double getFactorial (double a)
{
    double factorial;

    /**< Por convención, el factorial de 0 y 1 es 1 */
    if ((a == 0) || (a == 1))
        factorial = 1;
    else
        factorial = a * getFactorial(a - 1);

    return factorial;
}
Пример #9
0
/*Logic explanation: (Setting n = 5)
					 -Is 5 > 1? Yes.
					 -Then set local = to n * getFactorial(5-1) <I don't know what that is yet, but I'll run it>
					 -Is 4 > 1 now? Yes.
					 -Set local = n * getFactorial(4-1) <I still don't know it, I'll run it again...>
					 -Keeps going until if check returns false which should equal your "end game value", in this case, 1.
					 -On to:
					 	Is 2 > 1 now? Yes.
					 	Then set local = to n * getFactorial(2-1) <I don't know the value, I'll run it again>
					 	Is 1 > 1 now? No.
					 	Then return 1, but not before returning local total from previous loop.
					 		(Stack pops local = 2 * 1(current n) = 2,
					 		  		    local = 3 * 2(popped value) = 6,
					 				    local = 4 * 6(popped value) = 24,
					 				    local = 5 * 24(popped value) = 120,
					 			returns local = (value at bottom of stack) 120)

					 		Else if 1 was passed in, 1 > 1 will fall into else, returning 1.
					 (Note, there is still only one return in the function execution. Do NOT think this is creating
					 multiple returns for one function.  The function returns local only once, after n-1 loops, or
					 returns 1 if loop never executes.  In this case, the function runs 4 times, then returns loop.)
*/
unsigned int getFactorial(unsigned int n) //using unsigned int for larger values.
{
    unsigned int local;
    if(n > 1)
    {
        local = n * getFactorial(n-1);
        return local;
    } else
    {
        return 1;
    }
}
Пример #10
0
/* Consumer Thread */
void *T2(void *t) {
	printf("Consumer Started\n");
	struct LList *ll = (struct LList*) t;
	int queue_value;
	while (!done) {
		usleep(randSleep());
		pthread_mutex_lock(&count_mutex);
		if (size != 0) {
			/* Remove from Queue */
			queue_value = getEntry(ll);
			size--;
			printf("Consumer removed %d, computed %d! = %d, queue size = %d\n",
					queue_value, queue_value, getFactorial(queue_value), size);
			/* Pthread Mutex Locks */
		} else {
			pthread_cond_wait(&count_cond, &count_mutex);
		}
		pthread_mutex_unlock(&count_mutex);
	}
	pthread_exit(NULL);
}
Пример #11
0
int main(int argc, const char* argv[])
{
	getFactorial(100);

	return 0;
}