Thread: Decomposition of prime numbers (in C)

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Question Decomposition of prime numbers (in C)

    Hi, i am new in this, so please - be tolerant. Thanks
    I was trying to figure this task out:

    Task: Decomposition of prime numbers

    The standard input is given a positive integer (the integer range). Determine the prime
    decomposition of the given number (ie decomposition product of prime numbers) and write it to standard output as a product of prime factors with the power, as shown in the attached example. List primes in order from smallest to largest, do not write anywhere on the output gap.

    Sample Input 1:

    600

    Corresponding output:

    2 ^ 3 * 3 * 5 ^ 2

    Sample Input 2:

    1024

    Corresponding output:

    2 ^ 10

    Sample Input 3:

    77

    Corresponding output:

    7 * 11

    My code (not working, donīt know why):
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main(void)
    
    {
     int number;
     int i, repeat, x;
    
     int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 0};
    
     clrscr();
    
     printf("\nInput number:\n\n");
    
     scanf("%d", &number);
     fflush(stdin);
    
     printf("\n\nDecomposition of %d is ", number);
    
     i = 0;
    
     do{
    	x = number;
    
    	if(number == primes[i])
    	  {
    	   printf("%d", primes[i]);
    	   getch();
    	   return 1;
    	  }
    
    	if(number % primes[i] == 0)
    	  {
    	   printf("%d", primes[i]);
    	   repeat = 0;
    	   do{
    		  number = number / primes[i];
    		  repeat++;
    		 } while(number % primes[i] != 0);
    
    	   printf("^%d", repeat);
    	  }
    
    	if(number < primes[i + 1])
    	  {
    	   getch();
    	   return 1;
    	  }
    
    	if(number != x)
    	   printf("*");
    
    	i++;
       } while(primes[i] != 0);
    }
    Any ideas? Load the source code please.
    Last edited by Arch4Horseman; 12-04-2010 at 07:38 AM. Reason: Code tags

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, let's start from the beginning. Whomever is teaching you C doesn't really know any C him/herself.

    1) void main(void) should be int main(void) and should return 0.
    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

    2) NEVER fflush(stdin). It's undefined behaviour.
    Cprogramming.com FAQ > Why fflush(stdin) is wrong

    3) You have the right idea, but the logic is a bit cluttered. I think it would be best if you moved some of the functionality, such as the one detecting how many times a prime number divides your number(prime^n) in another function and just return the value from there.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Actually its himself and..uhm..he is a bit crazy.

    Anyway - thanks for post, i will do it better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime numbers program
    By Prestige in forum C Programming
    Replies: 3
    Last Post: 10-16-2010, 01:57 PM
  2. prime numbers and time consuming
    By Gustaff in forum C Programming
    Replies: 21
    Last Post: 08-30-2010, 01:29 PM
  3. Recuration Prime Numbers
    By megazord in forum C Programming
    Replies: 17
    Last Post: 05-17-2010, 08:56 AM
  4. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  5. Replies: 18
    Last Post: 11-04-2005, 02:41 PM