473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Number of zeros in a factorial number

4 New Member
It is given an integer "p". I have to find a number "n" for which "n factorial" has "p" numbers of zero at the end. Here is the solution i thought, but i am not sure if it is a solution for this problem. Do i have to make a function to calculate the factorial and another function to get the zeros?


Expand|Select|Wrap|Line Numbers
  1. int p;
  2. int count5=0;
  3. int i;
  4. int copy_i;
  5.  
  6. printf("Enter p: ");
  7. scanf("%d",&p);
  8.  
  9. for(i=1; ;i++)
  10. {
  11.     copy_i=i;
  12.  
  13.     while(copy_i/5)
  14.     {
  15.         if(copy_i%5==0)
  16.         {
  17.             count5++;
  18.             copy_i=copy_i/5;
  19.         }
  20.         else
  21.         {
  22.             break;
  23.         }
  24.     }
  25.  
  26.     if(count5==p)
  27.     {
  28.         printf("The minimum number n is: %d.",i);
  29.         break;
  30.     }
  31.     else if(count5>p) 
  32.     {
  33.         printf("No match for n! with %d zero.",p);
  34.         break;
  35.     }
  36.  
  37. }
  38.  
Aug 9 '12 #1
1 1945
donbock
2,426 Recognized Expert Top Contributor
Factorials get very big very fast. n! becomes too big to be represented in a C variable for depressingly small values of n. Besides that, a program to compute factorials can run disappointingly slow as n increases. Thus, I suggest you look for some alternative to the brute force algorithm suggested in your original post.

If a number ends in p zeroes, then it must be true that the number is divisible by 10^p. Find a list of distinct factors of 10^p and the largest value in that list will be the value n that you are looking for. Spend a lot of pencil-and-paper time to make sure your algorithm is correct before you write any code.
Aug 10 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
7
by: hasanainf | last post by:
Hi all, I have two querys QueryPurchased ProductID Location TotPcs Prod1 Loc1 100 Prod2 Loc1 50 Prod3 Loc1 150 Prod3 Loc3 150
1
by: | last post by:
How can I obtain the record number or row number for a record while in form view? Thank you in advance for any information. R. L.
14
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to...
2
by: c2h | last post by:
pls help me to solve this problem : using VB to convert a base 8 number into decimal number ..... thks
2
by: Hets | last post by:
Write a C program which can convert decimal number into binary number.
1
by: justB | last post by:
Hi I'm new to this. Could I get the C code to print the largest prime number of a number given by a user? I want to promt the user to input a valid number >0, and return its largest prime number. ...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
1
by: kwokv616 | last post by:
I'm currently working on a project to convert a Foxpro database into Access. How do I lookup a cell value (given row number and column number) and return it to a variable? This is what I want to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.