answersLogoWhite

0

Designing a non-recursive algorithm for factorial problem?

Updated: 8/19/2019
User Avatar

Wiki User

12y ago

Best Answer

This is a non-recursive implementation of n factorial that supports arbitrary length decimal arithmetic. For very large values of n, you may need to tell your linker to increase the size of the run-time stack.

#include <stdlib.h>

#include <stdio.h>

/* Portable arbitrary length decimal iterative */

/* one node of a linked list of digits, the first node being low-order */

struct _decimal {

int digit;

struct _decimal *next;

};

typedef struct _decimal decimal;

/* Portable arbitrary length decimal iterative */

/* Initialize the list - necessary on second pass, if main recoded */

void decimal_initialize (decimal *d, int n) {

decimal *next, *nextsave;

d->digit = n;

nextsave = d->next;

d->next = NULL;

next = nextsave;

while (next != NULL) {

nextsave = next->next;

free (next);

next = nextsave;

}

return;

}

/* Portable arbitrary length decimal iterative */

/* Append a digit at the high order position */

void decimal_add_digit (decimal *d, int n) {

decimal *new_digit = (decimal*) malloc (sizeof (decimal));

while (d->next != NULL) d = d->next;

new_digit->digit = n;

new_digit->next = NULL;

d->next = new_digit;

return;

}

/* Portable arbitrary length decimal iterative */

/* Print the digits in reverse order - recursive */

void decimal_print_digits (decimal *d, int last_digit) {

if (d->next != NULL) decimal_print_digits (d->next, false);

printf ("%d", d->digit);

if (last_digit) printf("\n");

return;

}

/* Portable arbitrary length decimal iterative */

/* multiply the list by N */

void decimal_multiply (decimal *d, int N) {

int carry = 0;

while (d != NULL) {

d->digit = d->digit * N + carry;

carry = d->digit / 10;

d->digit %= 10;

if (carry != 0 && d->next 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

/* Generates all variations to show differences in results */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Designing a non-recursive algorithm for factorial problem?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What does the word algorithm mean today?

An algorithm is the process by which you solve a problem


Why algorithm needs to solve programming problem?

This is the definition of an algorithm - a list of orders of how to solve a given programming problem.


What does an explanation mark mean after a number in a math problem?

Do you mean an exclaimation mark (!) An exclamination mark means factorial so............. 3! = 3 factorial 3 factorial means 1x2x3 = 6 2! or 2 factorial means 1x2 = 2 4! or 4 factorial means 1x2x3x4 = 24


Advantages of problem analysis and algorithm design over directly writing program in high-level language?

It is much easier to discover errors in a program that is well analyzed and well designed. Furthermore, a thoroughly analyzed and carefully designed program is much easier to follow and modify.Even the most experienced programmers spend a considerable amount of time analyzing a problem and designing an algorithm


What is an algorithm to calculate the sum of the digits of a three digit number?

algorithm is a way to solve your problem


What is recursive algorithm?

Algorithm can be defined as an interpretable, finite set of instructions for dealing with contigencies and accompanying task that has recognizable end-points for given inputs. It is a tool for solving a well computational problem. A recursive algorithm is one which calls itself.


What is anlgorithm in math?

An algorithm is a systematic method used to solve some problem.An algorithm is a systematic method used to solve some problem.An algorithm is a systematic method used to solve some problem.An algorithm is a systematic method used to solve some problem.


What is the sequence of steps necessary to solve a problem?

algorithm


What problem is solved by banker's algorithm?

deadlock avoidance


What is a step by step solution of a programming problem?

An algorithm.


How detailed does an algorithm need to be?

That depends on what the problem is that has to be solved.


When you choose iterative or recursive algorithm?

If you cannot find any iterative algorithm for the problem, you have to settle for a recursive one.