Finding Factorials and Ending zeros of a Factorial Number [closed] - c

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a problem. I want to find factorial of big numbers.
Ex: 1555! = ?.
195! = ?.
My main problem is that I want to know the exact number of ending 0's of the factorial numbers.
I use the following formula:
(m!)^n = m! = 2*10^(n-1) + 2^2 * 10^(n-2) + ------- + 2^n.
with this I can solve the other factorials for number of ending 0's like this.
100!= 2*10^1 + 2^2*10^0 = 20+4 = 24
100! has 24 ending 0's as per this calculation.
But, then I got other problem,
Ex: For 95!
i) 95! = (100 - 5)! = 24 - 2*5^(1-1) = 24 - 2 = 22 => 95! has 22 0's.
ii) 95! = (90 + 5)! = 9*(2*10^0) + 2*5^0)= 18+2 = 20 => 95! has 20 0's.
this is my problem. By using the above formula I got two different answers and I am confused, I don't get the perfect answer so please help me to find it.
Thank you...

The number of trailing zeros in n! is the number of factors of 5 in the sequence 1, 2, ..., n. This is because a trailing zeros is the number of factors of 10 in the result, and 10 has a prime factorisation of 5 x 2. There's always more factors of 2 than 5, so the number of 5's gives the result.
The number of factors of 5 is... [n/5] + [n/25] + ... + [n/(5^k)] + ... where [ ] means round down (floor).
What should the code look like to compute this? Something like this perhaps.
int trailing_factorial_zeros(int n) {
int result = 0;
int m5 = 5;
while (n >= m5) {
result += n / m5;
m5 *= 5;
}
return result;
}

This is a bad question, probably belongs on Math site anyway. But here's a thought for you:
First 100! = 100 * 99!
99! = 99 * 98! and so forth until
1! = 1, and 0! = 1.
You want to know how many trailing 0's are in N! (at least that is how I understand the question).
Think of how many are in 10!
10! = 3628800
so there are two. The reason why is because only 2*5 = a number with a trailing 0 along with 10. So we have a total of 2. (5*4 has a trailing 0 but 4 is a multiple of 2, and besides, we only get to multiply individual numbers once)
It is a good bet, then, that 20! has 4 (it does).
It's now your job to prove (or disprove) that this pattern will hold, and then come up with a way to code it.

Related

binomial coefficient for very high numbers in c

So the task I have to solve is to calculate the binomial coefficient for 100>=n>k>=1 and then say how many solutions for n and k are over an under barrier of 123456789.
I have no problem in my formula of calculating the binomial coefficient but for high numbers n & k -> 100 the datatypes of c get to small to calculated this.
Do you have any suggestions how I can bypass this problem with overflowing the datatypes.
I thought about dividing by the under barrier straight away so the numbers don't get too big in the first place and I have to just check if the result is >=1 but i couldn't make it work.
Say your task is to determine how many binomial coefficients C(n, k) for 1 ≤ k < n ≤ 8 exceed a limit of m = 18. You can do this by using the recurrence C(n, k) = C(n − 1, k) + C(n − 1, k − 1) that can visualized in Pascal's triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 (20) 15 6 1
1 7 (21 35 35 21) 7 1
1 8 (28 56 70 56 28) 8 1
Start at the top and work your way down. Up to n = 5, everything is below the limit of 18. On the next line, the 20 exceeds the limit. From now on, more and more coefficients are beyond 18.
The triangle is symmetric and strictly increasing in the first half of each row. You only need to find the first element that exceeds the limit on each line in order to know how many items to count.
You don't have to store the whole triangle. It is enough to keey the last and current line. Alternatively, you can use the algorithm detailed [in this article][ot] to work your way from left to right on each row. Since you just want to count the coefficients that exceed a limit and don't care about their values, the regular integer types should be sufficient.
First, you'll need a type that can handle the result. The larget number you need to handle is C(100,50) = 100,891,344,545,564,193,334,812,497,256. This number requires 97 bits of precision, so your normal data types won't do the trick. A quad precision IEEE float would do the trick if your environment provides it. Otherwise, you'll need some form of high/arbitrary precision library.
Then, to keep the numbers within this size, you'll want cancel common terms in the numerator and the denominator. And you'll want to calculate the result using ( a / c ) * ( b / d ) * ... instead of ( a * b * ... ) / ( c * d * ... ).

How to calculate function points [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
This is a question about theoretical computing. I have came through a question like below;
Consider a project with the following functional units :
Number of user inputs = 50
Number of user outputs = 40
Number of user enquiries = 35
Number of user files = 06
Number of external interfaces = 04
Assuming all complexity adjustment factors and weighing factors as average, the function points for the project will be;
The answer is 672. How is this calculated?
1. Typical complexity averages are as follows:
AVERAGE complexity weights = {4, 5, 4, 10, 7} for the 5 complexities respectively.
2. Typical Characteristic weights are as follows:
AVERAGE characteristic weight = 3.
3. Function point = FP = UFP x VAF
UFP = Sum of all the complexities i.e. the 5 parameters provided in the question,
VAF = Value added Factor i.e. 0.65 + (0.01 * TDI),
TDI = Total Degree of Influence of the 14 General System Characteristics.
Thus function points can be calculated as:
= (200 + 200 + 140 + 60 + 28) x (0.65 + (0.01 x (14 x 3))
= 628 x (0.65 + 0.42)
= 628 x (1.07)
= 672
Thus the function points for the project will be 672.
Checkout this article for a detailed walk-through into function-point calculations.

Using a for loop to generate elements of a vector

I am trying to compute with the equation
and I would like to store each value into a row vector. Here is my attempt:
multiA = [1];
multiB = [];
NA = 6;
NB = 4;
q = [0,1,2,3,4,5,6];
for i=2:7
multiA = [multiA(i-1), (factorial(q(i) + NA - 1))/(factorial(q(i))*factorial(NA-1))];
%multiA = [multiA, multiA(i)];
end
multiA
But this does not work. I get the error message
Attempted to access multiA(3); index out
of bounds because numel(multiA)=2.
multiA = [multiA(i-1), (factorial(q(i)
+ NA -
1))/(factorial(q(i))*factorial(NA-1))];
Is my code even remotely close to what I want to achieve? What can I do to fix it?
You don't need any loop, just use the vector directly.
NA = 6;
q = [0,1,2,3,4,5,6];
multiA = factorial(q + NA - 1)./(factorial(q).*factorial(NA-1))
gives
multiA =
1 6 21 56 126 252 462
For multiple N a loop isn't necessary neither:
N = [6,8,10];
q = [0,1,2,3,4,5,6];
[N,q] = meshgrid(N,q)
multiA = factorial(q + N - 1)./(factorial(q).*factorial(N-1))
Also consider the following remarks regarding the overflow for n > 21 in:
f = factorial(n)
Limitations
The result is only accurate for double-precision values of n that are less than or equal to 21. A larger value of n produces a result that
has the correct order of magnitude and is accurate for the first 15
digits. This is because double-precision numbers are only accurate up
to 15 digits.
For single-precision input, the result is only accurate for values of n that are less than or equal to 13. A larger value of n produces a
result that has the correct order of magnitude and is accurate for the
first 8 digits. This is because single-precision numbers are only
accurate up to 8 digits.
Factorials of moderately large numbers can cause overflow. Two possible approaches to prevent that:
Avoid computing terms that will cancel. This approach is specially suited to the case when q is of the form 1,2,... as in your example. It also has the advantage that, for each value of q, the result for the previous value is reutilized, thus minimizing the number of operations:
>> q = 1:6;
>> multiA = cumprod((q+NA-1)./q)
multiA =
6 21 56 126 252 462
Note that 0 is not allowed in q. But the result for 0 is just 1, so the final result would be just [1 multiA].
For q arbitrary (not necessarily of the form 1,2,...), you can use the gammaln function, which gives the logarithms of the factorials:
>> q = [0 1 2 6 3];
>> multiA = exp(gammaln(q+NA)-gammaln(q+1)-gammaln(NA));
>>multiA =
1.0000 6.0000 21.0000 462.0000 56.0000
You want to append a new element to the end of 'multiA':
for i=2:7
multiA = [multiA, (factorial(q(i) + NA - 1))/(factorial(q(i))*factorial(NA-1))];
end
A function handle makes it much simpler:
%define:
omega=#(q,N)(factorial(q + N - 1))./(factorial(q).*factorial(N-1))
%use:
omega(0:6,4) %q=0..6, N=4
It might be better to use nchoosek as opposed to factorial. The latter can overflow quite easily, I'd imagine.
multiA=nan(1,7);
for i=1:7
multiA(i)=nchoosek(q(i)+N-1, q(i));
end

How can i find the number of lowest possible square that can fit in the given square

let's suppose i have a square of 7x7.i can fill the square with other squares(i.e the squares of dimension 1x1,2x2.....6x6).How can i can fill the square with least possible smaller squares.please help me.
Consider a square with dimensions s x s. Cutting a smaller square of dimensions m x m out will result in a square of m x m, a square of n x n, and two rectangles of dimensions m x n, where m + n = s.
When s is even, the square can be divided such that m = n, in which case the rectangles will also be squares, resulting in an answer of 4.
However, when s is odd, values of m and n must be chosen such that the resulting rectangle can be filled with the least number of squares possible. There doesn't seem to be an immediately obvious way to figure out the best configuration, so I would suggest coming up with an algorithm to figure out the least number of squares that can be used to fill a rectangle of size m x n (this is a slightly simpler problem and I believe it can be solved with a recursive algorithm). The total number of squares needed will then be equal to 2 x ([number of squares in m x n rectangle] + 1). You can use a loop to check all the sizes of m between 1 and s/2.
Hope that gets you started.
Consider a square with dimensions s x s.
Factorialise s into primes. Then solve the problem for each prime sp. The answer will be the same for sp x sp as for s x s. It is probable that the smallest prime will give the lowest result. I have have no proof of this, but I have checked by hand up to 17 x 17.
This is a generalisation of Otaias notion of an even s resulting in an answer of 4.
Placiing algorithm:
You need to loop from n = (s+1)/2, rounded down, to n = s-1.
Put the n x n square in a corner.
Let m = s - n.
Place m x m squares in the adjacent corners and keep placing them until they (almost) reach the end of the n x n square.
The remaining space will be m x m (if you are lucky), or up to 2m-1 x 2m-1 with a corner piece missing.
Fill the remaining space with a similar algorithm. Start with placing a n2 x n2 square in the corner opposite to the missing corner piece.
Working by hand I have obtained the following results:
s minimum number of squares:
2 4
3 6
5 8
7 9
11 10
13 11
17 12
First check if n is even. If n is even, then the answer is four since there isn't a way to fit 3 squares or 2 squares together to make another square so that solves it for half of all possible cases
BEFORE YOU PROCEED: This approach is incomplete and this may be the WRONG approach
I just intend to throw out an out-of-the-box idea just because I feel like this may help and, hopefully, advance the problem. I feel like it may have some correlation with Goldbach's weak conjecture. The algorithm may be too long to compute for larger values, and I'm not sure how much optimization is happening.
Now my idea would be to try to enumerate all triples (n1,n2,n3) where n1 + n2 + n3 = n AND n1, n2, n3 are all prime (which are >= 2) AND n >= 7 AND n1 <= n2 <= n3
Now let me literally depict my algorithm:
Now my idea is find all possible triples (n1,n2,n3) so it fits the definition stated above. Next set n_s = n1 + n2. IF n_s > n3 follow the depiction above else flip n_s and n3
Now the problem is the white rectangles left over (that should be congruent to each other).
Let n4 x n3 denote the rectangles where:
n4 = n - 2 * n3 \\if following the depicted example
Enumerate all possible triples (n41, n42, n43) (treating n as n = n4, so n3 >= 7) and (n31, n32, n33) (treating n as n = n3, so n3 >= 7). Next find the value where n_s3 == n_s4 and both are the greatest they could be. For example:
Let's suppose x3 = 17 and x4 = 13
Enumeration of x3 = 17:
2 + 2 + 13
3 + 3 + 11
5 + 5 + 7
Enumeration of x_s3:
4 = 2 + 2
6 = 3 + 3
10 = 5 + 5
12 = 5 + 7
14 = 3 + 11
15 = 2 + 13
Enumeration of x4 = 13:
2 + 2 + 7
3 + 5 + 5
Enumeration of x_s4:
4 = 2 + 2
8 = 3 + 5
9 = 2 + 7
10 = 5 + 5
Since 10 is the largest value shared between 13 and 17, you fit a 10 by 10 square in the (both rectangles) and now you have a none parallelogram which get further and further more difficult to fill, but may be (I feel) towards the right direction.
All feed back appreciated.

Array containing n-2 integers 1 to n, O(n) algorithm to find missing 2 numbers with O(1) extra space? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find two missing numbers
I been thinking for a while and can't seem to get an answer to this... So an array with n-2 unique integers in the range from 1 to n and O(1) space in addition to the space used by the array is given. How can you find the two integers from 1 to n that is missing in the array in O(n) time?
So for example, a = [4,3,1,6] and O(1) extra space
How can you find 2, 5 in O(n) time?
Here's an idea: Just keep some statistic that gives you information about the missing numbers. For example, if you calculate the sum of all your numbers as S, then:
(1+2+..+N) - S = a+b
where a and b are your missing numbers. In your example, you get:
1+2+3+4+5+6 - 4+3+1+6 = 7 = a+b
You could then also do the same, for example, for multiplication and get:
(1*2*..*N) / S = a*b
in your case:
(1*2*3*4*5*6) / 72 = 10 = a*b
so the answer is 2 and 5.
Basically there are a lot of statistics you can use in this way...

Resources