Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>    // math library to use the sqrt() (returns the square root of a number)
  3.                     //and the ceil() (rounds up the float to the just higher integer
  4.                     //note: to compile properly we have to use the -lm flag to link the math library to the executable
  5.                     //ex: gcc prog.c -o out -lm
  6.  
  7.  
  8. void minimum(int min){
  9.  
  10.     printf("Minimum: %d\n",min);    
  11. }
  12.  
  13. void maximum(int max){
  14.  
  15.     printf("Maximum %d\n",max);    
  16. }
  17.  
  18. void printdivisors(int n){
  19.     for(int i = 1; i <= n/2; i++) {             //for every number i in the range [1,n/2] (if n is even) or [1,n/2-1] if n is odd
  20.                                                 //we check if the remainder of the division n/i is zero. If it is, i is a
  21.                                                 //divisor of n and we print it
  22.         if(n%i == 0){      
  23.             printf("%d is a divisor of %d\n",i,n);
  24.         }
  25.     }
  26.    
  27.     printf("%d is a divisor of %d\n",n,n);      //every number is a divisor of itself so we print the number as well
  28.    
  29. }
  30.  
  31. int checkprime(int n){ //returns 0 if number is not a prime, 1 if number is a prime
  32.     if(n == 1)
  33.         return 1;   //1 is a prime number
  34.     if(n==2)
  35.         return 1;   //2 is a prime number
  36.  
  37.     if(n%2 == 0)
  38.         return 0;   //if the number is divisible by 2 and is not 2 then it is not a prime number
  39.    
  40.     for(int i = 3; i <= ceil(sqrt(n)); i +=2){  //if a number is not prime, mathematics guarrantee that some factor other than
  41.                                                 //the number itself will be found in the range [2,sqrt(n)]. Since we have already
  42.                                                 //checked for the number two we begin from 3 and since we have already checked
  43.                                                 //out all of the even numbers we add 2 everytime to an odd number so that we
  44.                                                 //only check the odd numbers. Thus, we are saving time and effort.
  45.         if(n%i == 0){
  46.             return 0;
  47.         }
  48.     }
  49.  
  50.     return 1;
  51.    
  52. }
  53.  
  54. void printprimefactors(int n){
  55.     //printf("%d 's prime factors are: ",n);
  56.     for(int i = 2; i<= n/2; i++){   //in this function we combine the divisors of a number with the checkprime() function we
  57.                                     //already implemented. We simply find all of the divisors of a number and if they are prime
  58.                                     //we print them out
  59.         if(n%i == 0){
  60.             if(checkprime(i)){
  61.                 printf("%d ",i);
  62.             }
  63.         }
  64.     }
  65.  
  66.     printf("\n"); //print a newline in the end
  67.  
  68. }
  69.  
  70.  
  71. //this is the nightmare difficulty leve function
  72. //here we begin as in the printprimefactors() function by finding all of the prime divisors of the given number
  73. //we have a temporary variable temp in which we store the value of n for every iteration of the algorithm
  74. //everytime we find a prime factor, we divide temp by that factor until:
  75. //a)the division temp/i has a non zero remainder
  76. //b)the number temp becomes smaller than the prime factor
  77. //so, for every iteration of this algorithm we simply print out that factor however many times the algorithm runs
  78. //everytime we print that factor, we update the value of temp to be the result of the division temp/i
  79. //that way we can actually get how many times is that prime factor used, and since the factors are prime we
  80. //do not have to worry about their multiples
  81. //at the end of each factor cycle we restore temp to the original n value so that we don't use the small value
  82. //that was left behind from the previous computation
  83. void printadvprimefactors(int n){
  84.     //char* s = printprimefactors(n);
  85.     int temp = n;
  86.     for(int i = 2; i<= n/2;i++){
  87.         if(n%i == 0){
  88.             if(checkprime(i)){
  89.                 while(temp%i == 0 && temp>i){
  90.                     printf("%d ",i);
  91.                     temp = temp/i;
  92.                 }
  93.             }
  94.         }
  95.         temp = n;
  96.     }
  97.     printf("\n");
  98. }
  99.  
  100. int main(){
  101.     int min = 0;    //we use these two variables to hold the biggest and smallest number we enter
  102.     int max = 0;
  103.     int b;
  104.     scanf("%d",&b);
  105.     min = max = b;
  106.     minimum(min);
  107.     maximum(max);
  108.     printdivisors(b);
  109.     if(checkprime(b)){
  110.         printf("%d is a prime!\n",b);
  111.     }
  112.     printprimefactors(b);
  113.     printadvprimefactors(b);
  114.     //we do this once to initialize the min and max values with an initial value other than 0
  115.  
  116.     while(1){   //forever loop
  117.         int a;  //we read our input here
  118.         scanf("%d",&a);
  119.         //printf("%d\n", a);
  120.         if(a>max)
  121.             max = a;
  122.         if(min>a)
  123.             min = a;
  124.         minimum(min);
  125.         maximum(max);
  126.         printdivisors(a);
  127.         //printf("%d\n", checkprime(a));
  128.         if(checkprime(a)){                  //it was easier to implement the checkprime() function returning 1 and 0
  129.                                             //rather than having it print the whole string. that way we can reuse it in the
  130.                                             //printprimefactors and printadvprimefactors functions
  131.             printf("%d is a prime!\n", a);
  132.         }
  133.         printprimefactors(a);
  134.        
  135.         printadvprimefactors(a);
  136.     }
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement