Re:Factorial calculation
Posted: Mon Feb 23, 2004 7:50 am
you illiterate theologists!
here is the problem ..
you have to calculate n!
that is an integer, for those who haven't noticed
the calculation will complete after n steps
so time is not an issue (at least for n=73)
what apears to be our problem is
that in a language like c
no default arithmetic variable type is "large enough"
to handle the results' storage
after a little search:
http://www.google.com/search?hl=en&ie=U ... gle+Search
http://primes.utm.edu/links/programs/large_arithmetic/
using malloc, realloc for the implementation scheme is piece of cake
the hard biscuit would be to condact a multiplication
if memory serves correctly,
in digital circuits design a multiplication between two numbers in binary form, is farely trivial
since you either multiply with one (copy paste)
or zero (nullify the entire line)
perhaps some more ram might be needed
but unlikely you can find a faster algorithm
(i wonder what those arithmetic coprocessors are for)
now in regard with fibonacci,
which idiot would ever use top down recursion to solve it ??
after the first time you try,
you wont attempt another!
or you will be dead long before it ever finish ..
combinatorics! what a fabulous word, it makes a rhyme i think
you may have order, or disorder
element duplication, or clone elimination
for instance, when you wish to print the possible permutations of n objects to k boxes:
void foo(int n, int k, int p[], int i) {
int j;
if (i==k) { //permutation complete
printt(p,k); //p[] is an array of k boxes
return;
}
for (j=0; j<n; j++) { //n possible values..
p= j;
foo(n,k,p,i+1); //recursion!!
}
}
as you may have noticed, the easiest case (above) is the one
where you might run into printouts like 75667333333
or where 8254 is distinct from 4528
if on the other hand, are attempting to implement a set,
chaos mechanics rule:
void goo(int n, int k, int p[], int i, int j) {
if (i==k) {
...
}
for ( ; j<n; j++) {
p= j;
goo(n,k,p,i+1,p); //most amusing..
}
}
in the rare occasion when you dont wish for a value to be repeated, you must explicitly say so:
#define FREE 38745
#define TAKEN -845
void hoo(int n, int k, int p[], int i, int q[]) {
int j;
if (i==k) {
...
}
for (j=0; j<n; j++) {
if (q[j]==FREE) { //q[] is a log table of size n
q[j]= TAKEN;
p= j;
hoo(n,k,p,i+1,q);
q[j]= FREE; //dont forget!
}
}
} //q[] should have been initialized to FREE (u careless punks..)
the last case, of a set with no dublicates, is gracefully left as an exercise .. (usually n>=k, but you may find an exception)
naturally, the question still remains .. what do you want n! for ?!?
here is the problem ..
you have to calculate n!
that is an integer, for those who haven't noticed
the calculation will complete after n steps
so time is not an issue (at least for n=73)
what apears to be our problem is
that in a language like c
no default arithmetic variable type is "large enough"
to handle the results' storage
after a little search:
http://www.google.com/search?hl=en&ie=U ... gle+Search
http://primes.utm.edu/links/programs/large_arithmetic/
using malloc, realloc for the implementation scheme is piece of cake
the hard biscuit would be to condact a multiplication
if memory serves correctly,
in digital circuits design a multiplication between two numbers in binary form, is farely trivial
since you either multiply with one (copy paste)
or zero (nullify the entire line)
perhaps some more ram might be needed
but unlikely you can find a faster algorithm
(i wonder what those arithmetic coprocessors are for)
now in regard with fibonacci,
which idiot would ever use top down recursion to solve it ??
after the first time you try,
you wont attempt another!
or you will be dead long before it ever finish ..
combinatorics! what a fabulous word, it makes a rhyme i think
you may have order, or disorder
element duplication, or clone elimination
for instance, when you wish to print the possible permutations of n objects to k boxes:
void foo(int n, int k, int p[], int i) {
int j;
if (i==k) { //permutation complete
printt(p,k); //p[] is an array of k boxes
return;
}
for (j=0; j<n; j++) { //n possible values..
p= j;
foo(n,k,p,i+1); //recursion!!
}
}
as you may have noticed, the easiest case (above) is the one
where you might run into printouts like 75667333333
or where 8254 is distinct from 4528
if on the other hand, are attempting to implement a set,
chaos mechanics rule:
void goo(int n, int k, int p[], int i, int j) {
if (i==k) {
...
}
for ( ; j<n; j++) {
p= j;
goo(n,k,p,i+1,p); //most amusing..
}
}
in the rare occasion when you dont wish for a value to be repeated, you must explicitly say so:
#define FREE 38745
#define TAKEN -845
void hoo(int n, int k, int p[], int i, int q[]) {
int j;
if (i==k) {
...
}
for (j=0; j<n; j++) {
if (q[j]==FREE) { //q[] is a log table of size n
q[j]= TAKEN;
p= j;
hoo(n,k,p,i+1,q);
q[j]= FREE; //dont forget!
}
}
} //q[] should have been initialized to FREE (u careless punks..)
the last case, of a set with no dublicates, is gracefully left as an exercise .. (usually n>=k, but you may find an exception)
naturally, the question still remains .. what do you want n! for ?!?