VB icon

A prime factor program (UPDATED!)

Email
Submitted on: 1/7/2015 4:43:00 PM
By: PlanetCoder (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, Microsoft Visual C++, Borland C++
Views: 2397
 
     Program for calculating prime factors. Accepts very large numbers upto 2^53 -1 (9007199254740991)or more than 9 billion billion. Shows elasped time if time taken is 1 second or more. Largest prime accepted: 9007199254740881.

 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :A prime factor program (UPDATED!)
//**************************************
<stdio.h>
<math.h>
<time.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A prime factor program (UPDATED!)
// Description:Program for calculating prime factors. Accepts very large numbers upto 2^53 -1 (9007199254740991)or more than 9 billion billion. Shows elasped time if time taken is 1 second or more.
Largest prime accepted: 9007199254740881.
// By: PlanetCoder (from psc cd)
//**************************************

/*Prime.c: 
 	By Abdullah Chougle. Please VOTE at 
 http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9643&lngWId=3
 	Program for calculating prime factors. 
 Can accept nos. upto 2^53-1 (9007199254740991) or 9 billion billion. 
 Shows elasped time if time taken is 1 second or more.
 	Largest prime accepted: 9007199254740881
 */
 #include <stdio.h> /*include the standard input/output header file*/
 #include <math.h>/*include the math.h file for complex mathematical operations such as sqrt() and fmod()*/
 #include <time.h>/*include this file for calculating elapsed time*/
 void prime_no(long double); /*function for checking the inputted value, whether it is a decimal, or beyond the range.*/
 long double calc_prime(long double); /*returns the first prime factor of the number passed to it from the above function, or returns the number itself if it is 
prime*/
 main()
 {
 	long double prime; /*long double (and not "long int") is used for extra precision*/
 	while(1) /*infinite while loop, exited using a break statement*/
 	{
 		puts("Enter an integer to find its prime factors or enter 0 to quit:");
 		scanf("%Lf", &prime);
 		if(prime==0)
 			break; /*exiting the program*/
 		else
 			prime_no(prime); /*transferring control to this function*/
 	}
 }
 void prime_no(long double dx)
 {
 	long double pfact=1;/*where pfact denotes prime factor*/
 	long double x; 
 	int count=1;
 /****************TIME************************/
 time_t starts, finishes;
 time(&starts);
 /*********************TIME*******************/
 	/***ERROR CHECKING**/
 	if (dx <0)
 	{	
 		puts("You've entered a negative value. The minus sign will be ignored.");
 		dx = -dx;
 	}	
 	
 	if (floor(dx)!=dx && ceil(dx)!=dx) /*if rounded off value is not equal to inputted value*/
 	{
 		/**PRECISION CHECK**/
 		if(dx>9007199254740991) /*Max. precision of long double*/
 		{
 			puts("Sorry, this program can only handle numbers below 9007199254740991 (2^53 - 1)\n");
 			return;
 		}
 		
 		/***ERROR CHECKING**/
 		puts("This value contains a decimal fraction.\nOnly the integral part will be considered.");
 		dx=floor(dx);
 	}
 	
 	
 /****CALCULATING AND DISPLAYING THE VALUES****/
 	if(dx==1)
 		printf("\tThis number is neither prime nor composite");
 	else
 	{
 		do
 		{
 			if(count==1)
 			{
 				printf("\t");
 				x=dx/pfact;
 			}
 			else
 				x/=pfact;
 	
 			pfact=calc_prime(x);
 	
 			if (count>1)
 				printf(", ");
 			if(pfact==dx)
 				printf("This is a prime number.");
 			else
 				printf("%.0Lf", pfact);
 	
 			count++;
 		}while (pfact!=x); 
 	}
 	
 	
 /**********************TIME*********************/
 time(&finishes);
 if(difftime(finishes,starts)) /*if elapsed time is greater than 1 second, then display it*/
 	printf("\tElapsed time: %g sec", difftime(finishes, starts));
 /**********************TIME*********************/
 		
 	puts("\n");	
 	return;
 }
 //type long double is used for calculati
 // on of large values
 long double calc_prime(long double x)
 {
 	register unsigned long count; /*register is used in an attempt to increase calculation speed*/
 	
 	if (x==2)
 	{
 		return 2;
 	}
 	if (x>2)
 	{
 		
 		if (fmod(x,2)==0) /*if x is divisible by 2*/
 			return 2;
 		
 		for (count = 3; count <= sqrt(x); count+=2) /*try to divide the number by all odd integers upto x's square root until a factor is found*/
 		{
 			if (fmod(x,count)==0) /*where fmod denotes the remainder*/
 			{
 				return count;/*return the factor*/
 			}
 		}
 		return x;/*i.e. return the same number if it is prime.*/
 	}
 	
 }


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.