Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <math.h>
  5.  
  6. //Multiplication tables. 10*x and x*10 are erased because we are treating 1 digit factors multiplications
  7. int mult_tables[9][9]= {{ 1  , 2  , 3  , 4  , 5  , 6  , 7  , 8  , 9  },
  8.                         { 2  , 4  , 6  , 8  , 10 , 12 , 14 , 16 , 18 },
  9.                         { 3  , 6  , 9  , 12 , 15 , 18 , 21 , 24 , 27 },
  10.                         { 4  , 8  , 12 , 16 , 20 , 24 , 28 , 32 , 36 },
  11.                         { 5  , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 },
  12.                         { 6  , 12 , 18 , 24 , 30 , 36 , 42 , 48 , 54 },
  13.                         { 7  , 14 , 21 , 28 , 35 , 42 , 49 , 56 , 63 },
  14.                         { 8  , 16 , 24 , 32 , 40 , 48 , 56 , 64 , 72 },
  15.                         { 9  , 18 , 27 , 36 , 45 , 54 , 63 , 72 , 81 }};
  16.  
  17.  
  18. /*Function which return value is the last digit of the number given as parameter
  19.   e.g.:
  20.       last_digit(9) => 9
  21.       last_digit(154) => 4
  22. */
  23. int last_digit(int num){
  24.   int i = 0;
  25.   if(num <10) return num;
  26.   while((num%2 != 0) && (num%5 != 0)){
  27.     i++;
  28.     if((num - i)%2 == 0 && (num - i)%5 == 0){
  29.       return i;
  30.     }
  31.   }
  32.   return i;
  33. }
  34.  
  35.  
  36. /*Change the given number in (number -last_digit)/10
  37.   e.g.:
  38.       num_transf(154) => 15
  39. */
  40. int num_transf(int num, int dig){
  41.   return (num - dig)/10;
  42. }
  43.  
  44. int el(int n, int e){
  45.   for(int i=0; i<e; i++){
  46.     n *=n;
  47.   }
  48. }
  49.  
  50.  
  51.  
  52. long int mul(int p, int s){
  53.   int eli, elj;//Variables where to store the elevation counters
  54.   long int ret;//Variable to return
  55.   int sum;//Variable where to store the temporary result
  56.   int pd, sd;// Variables where to store the last digits of every factor
  57.   printf("SUCA");
  58.   for(int i=0; p!=0 ; i++){
  59.     pd = last_digit(p);
  60.     if(p >9) p = num_transf(p, pd);//If the number is has more than 1 digit, call the num_transf function
  61.     if(pd != 0){//If the digit is not 0
  62.       for(int j=0; s!=0; j++){
  63.         sd = last_digit(s);
  64.         if(s >9) s = num_transf(s, sd);//If the number is has more than 1 digit, call the num_transf function
  65.         if(sd != 0){//If the digit is not 0
  66.           elj= el(10,j);
  67.           sum += (mult_tables[pd-1][sd-1])*elj;
  68.         //Come devo gestire il riporto?
  69.         }
  70.       }
  71.       eli = el(10,i);
  72.       ret += sum*eli;
  73.     }
  74.     return ret;
  75.   }
  76.  
  77. }
  78.  
  79. int main(int argc, char** argv){
  80.   int n, m;
  81.   long int l;
  82.   printf("First factor: ");
  83.   scanf("%d", &n);
  84.   printf("\nSecond factor: ");
  85.   scanf("%d", &m);
  86.   l = mul(n,m);
  87.   printf("\nMultiplication result %d\n", n*m );
  88.   printf("Function result: %ld\n", l);
  89.   return 0;
  90.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement