Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <math.h>
- //Multiplication tables. 10*x and x*10 are erased because we are treating 1 digit factors multiplications
- int mult_tables[9][9]= {{ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 },
- { 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 },
- { 3 , 6 , 9 , 12 , 15 , 18 , 21 , 24 , 27 },
- { 4 , 8 , 12 , 16 , 20 , 24 , 28 , 32 , 36 },
- { 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 },
- { 6 , 12 , 18 , 24 , 30 , 36 , 42 , 48 , 54 },
- { 7 , 14 , 21 , 28 , 35 , 42 , 49 , 56 , 63 },
- { 8 , 16 , 24 , 32 , 40 , 48 , 56 , 64 , 72 },
- { 9 , 18 , 27 , 36 , 45 , 54 , 63 , 72 , 81 }};
- /*Function which return value is the last digit of the number given as parameter
- e.g.:
- last_digit(9) => 9
- last_digit(154) => 4
- */
- int last_digit(int num){
- int i = 0;
- if(num <10) return num;
- while((num%2 != 0) && (num%5 != 0)){
- i++;
- if((num - i)%2 == 0 && (num - i)%5 == 0){
- return i;
- }
- }
- return i;
- }
- /*Change the given number in (number -last_digit)/10
- e.g.:
- num_transf(154) => 15
- */
- int num_transf(int num, int dig){
- return (num - dig)/10;
- }
- int el(int n, int e){
- for(int i=0; i<e; i++){
- n *=n;
- }
- }
- long int mul(int p, int s){
- int eli, elj;//Variables where to store the elevation counters
- long int ret;//Variable to return
- int sum;//Variable where to store the temporary result
- int pd, sd;// Variables where to store the last digits of every factor
- printf("SUCA");
- for(int i=0; p!=0 ; i++){
- pd = last_digit(p);
- if(p >9) p = num_transf(p, pd);//If the number is has more than 1 digit, call the num_transf function
- if(pd != 0){//If the digit is not 0
- for(int j=0; s!=0; j++){
- sd = last_digit(s);
- if(s >9) s = num_transf(s, sd);//If the number is has more than 1 digit, call the num_transf function
- if(sd != 0){//If the digit is not 0
- elj= el(10,j);
- sum += (mult_tables[pd-1][sd-1])*elj;
- //Come devo gestire il riporto?
- }
- }
- eli = el(10,i);
- ret += sum*eli;
- }
- return ret;
- }
- }
- int main(int argc, char** argv){
- int n, m;
- long int l;
- printf("First factor: ");
- scanf("%d", &n);
- printf("\nSecond factor: ");
- scanf("%d", &m);
- l = mul(n,m);
- printf("\nMultiplication result %d\n", n*m );
- printf("Function result: %ld\n", l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement