ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • big_integer_operation(큰 정수 연산)
    Number Theory 2017. 3. 20. 22:47

     

    int 보다 큰 범위의 정수들을 기본연산해주는 함수들이다. (음수 연산 가능)

    기본 원리는 int 연산과 같다. 그러나 int는 32개의 bit라면 이 big_int는 32 * size 개의 bit이다.

    size가 1000이라면 약 자릿수 10000자리정도까지 계산 가능하다.

    main 함수에 적혀있는 것은 fibonacci 수열의 10만항을 구하도록 예제를 적어놓았다.

    size가 클 수록 연산의 속도가 눈에 띄게 감소한다.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    #include <stdio.h>
    #include <stdlib.h>
     
    #define size 5000
     
    typedef struct{
        unsigned int d[size];
    }big_int;
     
    //big_int를 생성해 주는 함수
    big_int* generate_big_int();
    //big_int를 지워주는 함수. 메모리 반환
    void delete_big_int(big_int *a);
    //big_int를init 값으로 초기화 시켜준다.
    void init_big_int(big_int* a, int init);
    //big_int를 입력받는 함수 a에 입력시킨다.
    void input_big_int(big_int* a);
    //big_int를 출력해주는 함수
    void print_big_int(big_int *a);
    //big_int 왼쪽으로 shift 시켜주는함수
    void lshift_big_int(big_int *a);
    //big_int 오른쪽으로 shift 시켜주는 함수.
    void rshift_big_int(big_int *a);
    //big_int를 2의 보수화 해주는 함수
    void complement_big_int(big_int *a);
    //big_int a값을 b에다 옮겨주는 함수. 즉 b = a;
    void assign_big_int(big_int *a, big_int *b);
    //c = a + b
    void add_big_int(big_int *a, big_int *b, big_int *c);
    //c = a - b
    void sub_big_int(big_int *a, big_int *b, big_int *c);
    //c = a * b
    void mul_big_int(big_int *a, big_int *b, big_int *c);
    //a와b 값이 unsigned라고 했을 때 둘을 비교해주는 함수.
    int cmp_unsigned_big_int(big_int *a, big_int *b);
    //a와 b 값이 signed 라고 했을 때 둘을 비교해주는 함수.
    int cmp_signed_big_int(big_int *a, big_int *b);
    //c = a / b
    void div_big_int(big_int *a, big_int *b, big_int *c);
    //c = a % b
    void mod_big_int(big_int *a, big_int *b, big_int *c);
     
    int main()
    {
        big_int *a, *b, *c;
        int i;
     
        a = generate_big_int();
        b = generate_big_int();
        c = generate_big_int();
     
        init_big_int(a, 0);
        init_big_int(b, 1);
        for (i = 0; i < 100000; i++)
        {
            add_big_int(a, b, c);
            assign_big_int(b, a);
            assign_big_int(c, b);
        }
        print_big_int(c);
        printf("\n");
     
        delete_big_int(a);
        delete_big_int(b);
     
        return 0;
    }
     
    big_int* generate_big_int()
    {
        int i;
        big_int *a = (big_int*)malloc(sizeof(big_int));
     
        for (i = 0; i < size; i++)
            a->d[i] = 0;
     
        return a;
     
    }
     
    void delete_big_int(big_int *a)
    {
        free(a);
    }
     
    void init_big_int(big_int* a, int init)
    {
        int sign = init & 0x80000000;
        int i;
     
        if (sign)
            sign = 0x77777777;
        else
            sign = 0x00000000;
     
        for (i = 0; i < size - 1; i++)
            a->d[i] = sign;
        a->d[i] = init;
     
    }
     
    void input_big_int(big_int* a)
    {
        char *buffer;
        int i,j,k;
        int n = 0;
        int sign = 0;
        int carry = 0;
     
        buffer = (char*)malloc(sizeof(char)* 10 * size + 1);
     
        printf("please input integer : ");
     
        for (i = 0; i <= size * 10 && (buffer[i] = getchar()) != '\n'; i++)        // buffer[i]에는'\n' 이 저장
            buffer[i] -= '0';
     
        if (buffer[0] == '-' - '0')
            sign = 1;
     
        k = sign;
        while (k != i)
        {
            carry = 0;
            for (j = k; j < i; j++)
            {
                buffer[j] = (carry + buffer[j]);
                if (buffer[j] % 2)
                    carry = 10;
                else
                    carry = 0;
                buffer[j] /= 2;
            }
     
            if (carry)
            {
                a->d[size - 1 - n / 32] +=  0x00000001 << (n%32);
            }
            n++;
     
            if (buffer[k] == 0)
                k++;
        }
     
        if (sign)
            complement_big_int(a);
     
        free(buffer);
        return;
    }
     
    void print_big_int(big_int *a)
    {
        char *buffer;
        int sign = a->d[0] & 0x80000000;
        int i,j,k;
        int n=1; //n은 숫자열 크기
        int carry;
     
        buffer = (char*)malloc(sizeof(char)* 10 * size + 1);
     
        buffer[0] = 0;
     
        if (sign)
        {
            printf("-");
            complement_big_int(a);
        }
     
        for (i = 0; i < size; i++)
        {
            for (j = 0; j < 32; j++)
            {
                carry = 0;
     
                for (k = 0; k <n ; k++)
                {
                    buffer[k] = buffer[k] * 2 + carry;
                    carry = buffer[k] / 10;
                    buffer[k] = buffer[k] % 10;
                }
     
                if (carry)
                    buffer[n++] = 1;
     
                if (a->d[i] & (0x80000000 >> j))
                    buffer[0]++;
            }
        }
     
        for (i = n - 1; i >= 0; i--)
            printf("%c", buffer[i] + '0');
     
        if (sign)
            complement_big_int(a);
     
        free(buffer);
    }
     
    void lshift_big_int(big_int *a)
    {
        int i;
        int carry = 0;
        int temp;
     
        for (i = size - 1; i >= 0; i--)
        {
            temp = carry;
            if (a->d[i] & 0x80000000)
                carry = 1;
            else
                carry = 0;
     
            a->d[i] = a->d[i] << 1;
            a->d[i] += temp;
        }
    }
     
    void rshift_big_int(big_int *a)
    {
        int i;
        int carry = 0;
        int temp;
     
        for (i = 0; i <size; i++)
        {
            temp = carry;
            if (a->d[i] & 0x00000001)
                carry = 0x80000000;
            else
                carry = 0x00000000;
     
            a->d[i] = a->d[i] >> 1;
            a->d[i] += temp;
        }
    }
     
    void complement_big_int(big_int *a)
    {
        int i,j;
     
        for (i = 0; i < size; i++)
            a->d[i] = ~a->d[i];
        
        for (i = size - 1; i >= 0; i--)
        {
            for (j = 0; j < 32; j++)
            {
                if (a->d[i] & (0x00000001 << j))
                {
                    a->d[i] = a->d[i] - (0x00000001 << j);
                }
                else
                {
                    a->d[i] = a->d[i] + (0x00000001 << j);
                    return;
                }
                
            }
        }
    }
     
    void assign_big_int(big_int *a, big_int *b)
    {
        for (int i = 0; i < size; i++)
            b->d[i] = a->d[i];
    }
     
    void add_big_int(big_int *a, big_int *b, big_int *c)
    {
        int i;
        int carry = 0;
        unsigned long long temp;
        
        for (i = size - 1; i >= 0;i--)
        {
            temp = (unsigned long long)a->d[i] + (unsigned long long)b->d[i] + carry;
            if (temp >= 0x0000000100000000)
                carry = 1;
            else
                carry = 0;
            c->d[i] = (unsigned int)temp;
        }
    }
     
    void sub_big_int(big_int *a, big_int *b, big_int *c)
    {
        int i;
        unsigned long long borrow = 0;
     
        for (i = size - 1; i >= 0; i--)
        {
            if ((unsigned long long)a->d[i] >= (unsigned long long)b->d[i] + borrow)
            {
                c->d[i] = a->d[i] - b->d[i]- borrow;
                borrow = 0;
            }
            else
            {
                c->d[i] = a->d[i] - b->d[i] - borrow;
                borrow = 1;
            }
        }
     
    }
     
    void mul_big_int(big_int *a, big_int *b, big_int *c)
    {
        big_int *temp_a, *temp_b;
        int i, j,k;
        unsigned long long temp;
        unsigned long long carry;
     
        temp_a = generate_big_int();
        temp_b = generate_big_int();
        assign_big_int(a, temp_a);
        assign_big_int(b, temp_b);
     
        for (i = size - 1; i >= 0; i--)
            c->d[i] = 0;
     
        for (i = size - 1; i >= 0; i--)
        {
            carry = 0;
     
            for (j = size - 1,k = i ; j >= 0 && k>=0; j--,k--)
            {
                temp = (unsigned long long)temp_a->d[j] * (unsigned long long)temp_b->d[i] + (unsigned long long)c->d[k] + carry;
                c->d[k] = (unsigned int)temp;
                carry = temp >> 32;
            }
        }
     
        delete_big_int(temp_a);
        delete_big_int(temp_b);
    }
     
    int cmp_unsigned_big_int(big_int *a, big_int *b)
    {
        int i;
     
        for (i = 0; i < size; i++)
        {
            if (a->d[i] > b->d[i])
                return 1;
            if (a->d[i] < b->d[i])
                return -1;
        }
        return 0;
    }
     
    int cmp_signed_big_int(big_int *a, big_int *b)
    {
        int i;
     
        if (!(a->d[0] & 0x80000000) && (b->d[0] & 0x80000000))
            return 1;
        else if ((a->d[0] & 0x80000000) && !(b->d[0] & 0x80000000))
            return -1;
        else if (!(a->d[0] & 0x80000000) && !(b->d[0] & 0x80000000))
        {
            for (i = 0; i < size; i++)
            {
                if (a->d[i] > b->d[i])
                    return 1;
                if (a->d[i] < b->d[i])
                    return -1;
            }
        }
        else
        {
            for (i = 0; i < size; i++)
            {
                if (a->d[i] > b->d[i])
                    return -1;
                if (a->d[i] < b->d[i])
                    return 1;
            }
        }
     
        return 0;
    }
     
    void div_big_int(big_int *a, big_int *b, big_int *c)
    {
        big_int *temp_a, *temp_b;
        int a_sign = a->d[0] & 0x80000000;
        int b_sign = b->d[0] & 0x80000000;
        int i;
     
        temp_a = generate_big_int();
        temp_b = generate_big_int();
        assign_big_int(a, temp_a);
        assign_big_int(b, temp_b);
     
        for (i = size - 1; i >= 0; i--)
            c->d[i] = 0;
     
        if (a_sign)
            complement_big_int(temp_a);
        if (b_sign)
            complement_big_int(temp_b);
     
        i = 0;
        while (!(temp_b->d[0] & 0x80000000))
        {
            lshift_big_int(temp_b);
            i++;
        }
     
        for (; i >= 0; i--)
        {
            if (cmp_unsigned_big_int(temp_a, temp_b) >= 0)
            {
                c->d[size - 1 - i / 32] = c->d[size - 1 - i / 32] + (0x00000001 << (i % 32));
                sub_big_int(temp_a, temp_b, temp_a);
            }
            rshift_big_int(temp_b);
        }
     
        if (a_sign ^ b_sign)
            complement_big_int(c);
     
        delete_big_int(temp_a);
        delete_big_int(temp_b);
    }
     
    void mod_big_int(big_int *a, big_int *b, big_int *c)
    {
        big_int *temp_a, *temp_b;
        int a_sign = a->d[0] & 0x80000000;
        int b_sign = b->d[0] & 0x80000000;
        int i;
     
        temp_a = generate_big_int();
        temp_b = generate_big_int();
        assign_big_int(a, temp_a);
        assign_big_int(b, temp_b);
     
        for (i = size - 1; i >= 0; i--)
            c->d[i] = 0;
     
        if (a_sign)
            complement_big_int(temp_a);
        if (b_sign)
            complement_big_int(temp_b);
     
        i = 0;
        while (!(temp_b->d[0] & 0x80000000))
        {
            lshift_big_int(temp_b);
            i++;
        }
     
        for (; i >= 0; i--)
        {
            if (cmp_unsigned_big_int(temp_a, temp_b) >= 0)
            {
                c->d[size - 1 - i / 32] = c->d[size - 1 - i / 32] + (0x00000001 << (i % 32));
                sub_big_int(temp_a, temp_b, temp_a);
            }
            rshift_big_int(temp_b);
        }
     
        assign_big_int(temp_a, c);
     
        if (a_sign)
            complement_big_int(c);
     
        delete_big_int(temp_a);
        delete_big_int(temp_b);
    }

Designed by Tistory.