Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <stddef.h>
- #include "file.h"
- void output (struct long_num c){
- int i;
- if (c.sigh == 1)
- printf ("+");
- else
- printf ("-");
- for (i = c.lengh-1 ; i >= 0; i--){
- printf ("%d", c.c[i]);
- }
- printf (" \n" );
- }
- struct long_num first (int a){
- int sigh;
- if (a < 0){
- sigh = -1;
- a = -a;
- }
- else
- sigh = 1;
- int n, i = 0;
- n = a;
- struct long_num A;
- while (n != 0){
- n = n/10;
- i++;
- }
- if (( A.c = malloc ( i * sizeof ( int))) == NULL )
- exit (1);
- A.lengh = i;
- for (i = 0; i <= A.lengh; i++){
- A.c[i] = 0;
- }
- i=0;
- while (a != 0){
- A.c[i] = a%10;
- a = a/10;
- i++;
- }
- A.sigh = sigh;
- return A;
- }
- struct long_num summa_moduley (struct long_num A1, struct long_num A2){
- struct long_num sum;
- if (A1.lengh > A2.lengh){
- sum.lengh = A1.lengh + 1;
- }
- else{
- sum.lengh = A2.lengh + 1;
- }
- if (( sum.c = malloc ( sum.lengh * sizeof ( int ))) == NULL ){
- exit (1);
- }
- int i=0, count = 0, k=0;
- for (i = 0; i < sum.lengh; i++){
- sum.c[i] = 0;
- }
- if (A1.lengh >= A2.lengh){
- for (i = 0; i < A2.lengh; i++){
- sum.c[i] = A1.c[i] + A2.c[i] + count;
- if (sum.c[i] >= 10){
- count = 1;
- sum.c[i] = sum.c[i] - 10;
- }
- else
- count = 0;
- }
- for (i = A2.lengh; i <A1.lengh; i++){
- sum.c[i] = A1.c[i] + count;
- if (sum.c[i] >= 10){
- count = 1;
- sum.c[i] = sum.c[i] - 10;
- }
- else
- count = 0;
- }
- if (count == 1)
- sum.c[i]++;
- }
- else{
- for (i = 0; i < A1.lengh; i++){
- sum.c[i] = A1.c[i] + A2.c[i] + count;
- if (sum.c[i] >= 10){
- count = 1;
- sum.c[i] = sum.c[i] - 10;
- }
- else{
- count = 0;
- }
- }
- for (i = A1.lengh ; i < A2.lengh; i++){
- sum.c[i] = A2.c[i] + count;
- if (sum.c[i] >= 10){
- count = 1;
- sum.c[i] = sum.c[i] - 10;
- }
- else{
- count = 0;
- }
- }
- if (count == 1)
- sum.c[i]++;
- }
- sum.sigh = 1;
- return sum;
- }
- struct long_num subtraction_moduley (struct long_num A1, struct long_num A2){
- struct long_num subtr;
- int count=0, i=0;
- if (A1.lengh > A2.lengh){
- subtr.lengh = A1.lengh;
- }
- else{
- subtr.lengh = A2.lengh;
- }
- if (( subtr.c = malloc ( subtr.lengh * sizeof ( int ))) == NULL )
- exit (1);
- for (i = 0; i < subtr.lengh; i++){
- subtr.c[i] = 0;
- }
- int kill=11;
- if (A1.lengh == A2.lengh){
- i = A1.lengh-1;
- kill = 0;
- while ((A1.c[i] == A2.c[i])&&(i!=-1)){
- i--;
- }
- if (i < 0){
- kill = 0;
- subtr.lengh = 1;
- subtr.c[0] = 0;
- subtr.c[1] = 0;
- subtr.sigh = 1;
- return subtr;
- }
- if (A1.c[i] > A2.c[i]){
- kill = 1;
- }
- else{
- kill = 2;
- }
- }
- if ((A1.lengh > A2.lengh) || (kill == 1)){
- count = 0;
- for (i = 0; i < A2.lengh; i++){
- subtr.c[i] = A1.c[i] - A2.c[i] - count;
- if (subtr.c[i] < 0){
- subtr.c[i] += 10;
- count = 1;
- }
- else
- count = 0;
- }
- for (i = A2.lengh; i < A1.lengh ; i++){
- subtr.c[i] = A1.c[i] - count;
- if (subtr.c[i] < 0){
- subtr.c[i] += 10;
- count = 1;
- }
- else
- count = 0;
- }
- subtr.sigh = 1;
- }
- else{
- for (i = 0; i < A1.lengh; i++){
- subtr.c[i] = A2.c[i] - A1.c[i] - count;
- if (subtr.c[i] < 0){
- subtr.c[i] += 10;
- count = 1;
- }
- else
- count = 0;
- }
- for (i = A1.lengh; i < A2.lengh ; i++){
- subtr.c[i] = A2.c[i] - count;
- if (subtr.c[i] < 0){
- subtr.c[i] += 10;
- count = 1;
- }
- else
- count = 0;
- }
- subtr.sigh = -1;
- }
- return subtr;
- }
- struct long_num summa (struct long_num A1, struct long_num A2){
- struct long_num sum;
- if((A1.sigh == 1) && (A2.sigh == 1)){ //1
- sum = summa_moduley (A1, A2);
- }
- if((A1.sigh == -1) && (A2.sigh == -1)){ //2
- sum = summa_moduley (A1, A2);
- sum.sigh = -sum.sigh;
- }
- if((A1.sigh == 1) && (A2.sigh == -1)){ //3
- sum = subtraction_moduley (A1, A2);
- }
- if((A1.sigh == -1) && (A2.sigh == 1)){ //4
- sum = subtraction_moduley (A2, A1);
- // sum.sigh = -sum.sigh;
- }
- // if (sum.c[sum.lengh]==0) sum.lengh--;
- // printf ("this is sum: ");
- // output (sum);
- return sum;
- }
- struct long_num subtraction (struct long_num A1, struct long_num A2){
- struct long_num subtr;
- if((A1.sigh == 1) && (A2.sigh == 1)){ //1
- subtr = subtraction_moduley (A1, A2);
- }
- if((A1.sigh == -1) && (A2.sigh == -1)){ //2
- subtr = subtraction_moduley (A1, A2);
- subtr.sigh = -subtr.sigh;
- }
- if((A1.sigh == 1) && (A2.sigh == -1)){ //3
- subtr = summa_moduley (A1, A2);
- }
- if((A1.sigh == -1) && (A2.sigh == 1)){ //4
- subtr = summa_moduley (A1, A2);
- subtr.sigh = -subtr.sigh;
- }
- printf ("subtraction lengh %d\n", subtr.lengh);
- return subtr;
- }
- struct long_num zeros (struct long_num B1, struct long_num B2){
- int b, i;
- struct long_num A1;
- A1.sigh = B1.sigh;
- if (B1.lengh > B2.lengh){
- b = ceil(log2(B1.lengh));
- printf ("b = %d", b);
- if (B1.lengh == 1)
- b = 1;
- // printf ("b %d \n", b);
- A1.lengh = pow (2, b);
- printf ("lengh = %d", A1.lengh);
- if (( A1.c= malloc ( A1.lengh * sizeof ( int ))) == NULL )
- exit (1);
- for (i = 0; i < B1.lengh; i++)
- A1.c[i] = B1.c[i];
- for (i = B1.lengh; i < A1.lengh; i++)
- A1.c[i] = 0;
- }
- else{
- b = ceil(log2(B2.lengh));
- if (B2.lengh == 1)
- b = 1;
- A1.lengh = pow (2, b);
- // printf ("b %d \n", b);
- if (( A1.c = malloc ( A1.lengh * sizeof ( int ))) == NULL )
- exit (1);
- for (i = 0; i < B1.lengh; i++)
- A1.c[i] = B1.c[i];
- for (i = B1.lengh; i < A1.lengh; i++)
- A1.c[i] = 0;
- }
- return A1;
- }
- struct long_num kara(struct long_num B1, struct long_num B2){ // n=A1.lengh
- printf ("u are in karatzuba\n");
- output (B1);
- printf ("lengh B1 %d\n", B1.lengh);
- output (B2);
- printf ("lengh B2 %d\n", B2.lengh);
- int k1 = B1.lengh-1;
- while (B1.c[k1]==0){
- k1--;
- B1.lengh --;
- printf("XYI 1\n");
- }
- int k2 = B2.lengh-1;
- while (B2.c[k2]==0){
- k2--;
- B2.lengh --;
- printf("XYI 2\n");
- }
- output (B1);
- printf ("lengh B1 %d\n", B1.lengh);
- output (B2);
- printf ("lengh B2 %d\n", B2.lengh);
- printf ("u are in karatzuba\n");
- output (B1);
- printf ("lengh B1 %d\n", B1.lengh);
- output (B2);
- printf ("lengh B2 %d\n", B2.lengh);
- struct long_num Q1;
- struct long_num Q2;
- int j;
- if (B1.lengh > B2.lengh){
- if (B1.lengh%2 == 0){
- Q1.lengh = B1.lengh;
- Q2.lengh = B1.lengh;
- }
- else{
- Q1.lengh = B1.lengh+1;
- Q2.lengh = B1.lengh+1;
- }
- if (( Q1.c = malloc ( Q1.lengh * sizeof ( int ))) == NULL )
- exit (1);
- if (( Q2.c = malloc ( Q2.lengh * sizeof ( int ))) == NULL )
- exit (1);
- if (B1.lengh%2 == 1){
- Q1.c[B1.lengh] = 0;
- Q2.c[B1.lengh] = 0;
- }
- for(j = 0; j < B2.lengh; j++){
- Q1.c[j] = B1.c[j];
- Q2.c[j] = B2.c[j];
- }
- for(j = B2.lengh; j < B1.lengh; j++){
- Q1.c[j] = B1.c[j];
- Q2.c[j] = 0;
- }
- }
- else{
- if (B2.lengh%2 == 0){
- Q1.lengh = B2.lengh;
- Q2.lengh = B2.lengh;
- }
- else{
- Q1.lengh = B2.lengh+1;
- Q2.lengh = B2.lengh+1;
- }
- if (( Q1.c = malloc ( Q1.lengh * sizeof ( int ))) == NULL )
- exit (1);
- if (( Q2.c = malloc ( Q2.lengh * sizeof ( int ))) == NULL )
- exit (1);
- if (B2.lengh%2 == 1){
- Q1.c[B2.lengh] = 0;
- Q2.c[B2.lengh] = 0;
- }
- for(j = 0; j < B1.lengh; j++){
- Q1.c[j] = B1.c[j];
- Q2.c[j] = B2.c[j];
- }
- for(j = B1.lengh; j < B2.lengh; j++){
- Q1.c[j] = 0;
- Q2.c[j] = B2.c[j];
- }
- }
- Q1.sigh = B1.sigh;
- Q2.sigh = B2.sigh;
- printf ("lengh Q1 %d\n", Q1.lengh);
- output (Q1);
- printf ("lengh Q2 %d\n", Q2.lengh);
- output (Q2);
- int n = Q1.lengh;
- int k = n/2;
- if (n<=2){
- printf ("u are in n<=2\n");
- int answer1 = Q1.c[0];
- int answer2 = Q2.c[0];
- answer1 = answer1 + Q1.c[1] * 10;
- answer2 = answer2 + Q2.c[1] * 10;
- struct long_num Ans;
- Ans = first (answer1*answer2);
- Ans.sigh = Q1.sigh * Q2.sigh;
- printf ("Answer: ");
- output (Ans);
- return Ans;
- }
- else{
- printf ("good, but u are in n>2\n");
- struct long_num X1;
- struct long_num X2;
- struct long_num Y1;
- struct long_num Y2;
- X1 = half_power (Q1);
- X2 = half (Q1);
- Y1 = half_power (Q2);
- Y2 = half (Q2);
- printf ("after do half ");
- struct long_num p1;
- struct long_num p2;
- struct long_num s1;
- struct long_num s2;
- printf ("again karatzuba\n");
- p1 = kara (X1, Y1);
- p2 = kara (X2, Y2);
- printf ("not bad, but sum now!\n");
- // output (Y1);
- // output (Y2);
- s1 = summa (X1, X2);
- s2 = summa (Y1, Y2);
- // printf ("what we want sum:\n");
- // printf ("x1 lengh = %d\n", X1.lengh);
- // printf ("x2 lengh = %d\n", X2.lengh);
- // printf ("sum lengh = %d\n", s1.lengh);
- printf ("value of sum: ");
- output (s1);
- output (s2);
- struct long_num XY1;
- struct long_num XY2;
- struct long_num XY3;
- struct long_num XY4;
- struct long_num XY5;
- struct long_num XY6;
- struct long_num XY;
- printf ("now is power for this lengh: ");
- XY1 = powering (p1, 2*k);
- output (XY1);
- XY2 = kara (s1, s2);
- printf ("before subtraction we must new what we subtract: \n");
- output (XY2);
- output (p1);
- XY3 = subtraction (XY2, p1);
- printf ("this is subtraction: ");
- output (XY3);
- printf ("before subtraction we must new what we subtract: \n");
- output (XY3);
- output (p2);
- XY4 = subtraction (XY3, p2);
- printf ("this is subtraction: ");
- output (XY4);
- printf ("hi, power half lengh\n");
- XY5 = powering (XY4, k);
- output (XY5);
- printf ("summa of: ");
- output (XY1);
- output (XY5);
- XY6 = summa (XY1, XY5);
- printf ("and result is: ");
- output (XY6);
- printf ("summa of: ");
- output (XY6);
- output (p2);
- printf ("and result is: ");
- XY = summa (XY6, p2);
- output (XY);
- return XY;
- //xy = kara(X1,Y1) * pow(10, 2*n) + ((kara(summa(X1,X2),summa(Y1,Y2))) - kara(X1,Y1) - kara(X2,Y2)) * pow(10, n) + kara(X2,Y2);
- }
- }
- struct long_num half_power (struct long_num X){
- struct long_num X1;
- X1.sigh = X.sigh;
- X1.lengh = X.lengh/2;
- if (( X1.c = malloc ( X1.lengh * sizeof ( int ))) == NULL ){
- exit (1);
- }
- int i;
- for (i = 0; i < X.lengh/2; i++){
- X1.c[i] = X.c[i+X.lengh/2];
- }
- printf ("half_power: ");
- output (X1);
- return X1;
- }
- struct long_num half (struct long_num X){
- struct long_num X2;
- X2.sigh = X.sigh;
- X2.lengh = X.lengh/2;
- if (( X2.c = malloc ( X2.lengh * sizeof ( int ))) == NULL ){
- exit (1);
- }
- int i;
- for (i = 0; i < X.lengh/2; i++){
- X2.c[i] = X.c[i];
- }
- printf ("half: ");
- output (X2);
- return X2;
- }
- struct long_num powering (struct long_num A, int n){
- struct long_num B;
- B.lengh = n + A.lengh;
- if (( B.c = malloc ( B.lengh * sizeof ( int ))) == NULL )
- exit (1);
- B.sigh = A.sigh;
- int i;
- for (i = 0; i < n; i++){
- B.c[i] = 0;
- }
- for (i = n; i < B.lengh; i++){
- B.c[i] = A.c[i-n];
- }
- return B;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement