alu_mathics wrote:For my w.a. solution i have to change the long data into long long.
Formula:
The number of occurence of a prime factor in n! is
summation (floor(n/pow(f,i))) where i=1,2,3.... and pow(f,i)<=n;
while calculating the value of pow(f,i) my previous code gives data overflow.Do you consider that case?Hope so.
I really needed that calm stating of a way to count the occurances. Thank you.
I would like to add that long long is not necessary to avoid overflow. Consider
this overflowing algorithm:
- Code: Select all
/* Gives f's occurances in n! */
int multiplicity_in_factorial(int f, int n) {
int sum = 0, fpot = f;
while(fpot <= n) {
sum += n / fpot;
fpot *= f;
}
return sum;
}
Yes I'm very sloppy by using int. My and Valladolid's MAXINT is 2147483647 today.
Anyway, when n = MAXINT, fpot overflows since fpot > MAXINT becomes the loop
stopping criterion.
In order not to multiply fpot with f if the product is greater than MAXINT we just
have to divide MAXINT by f and use that for comparison before we multiply. And
furthermore, we don't need to know MAXINT. n fits in our datatype, no? Well, don't
ever go beyond n.
- Code: Select all
/* Gives f's occurances in n! */
int multiplicity_in_factorial(int f, int n) {
int sum = 0, fpot = 1;
int fpotlim = n / f;
while(fpot <= fpotlim) {
fpot *= f;
sum += n / fpot;
}
return sum;
}
Totally wrong? A little correct? Please?