# Project Euler -- 13. Larger Sum summary, euler13.larger

Source: Internet
Author: User

Project Euler -- 13. Larger Sum summary, euler13.larger

Today, it took a day to complete Larger Sum programming for the Euler's 13th question in Linux (it took a little time ). Since it took such a long time, we should make a good summary, Or I'm sorry for the time of this day. This time it was mainly programmed on ubantu, and it was designed to compile, debug, link, and compile Makefile.

Larger Sum involves the calculation of a very large number (50 bits). In C compilation language, int Is 32 bits, and the maximum integer long int Is only pow) it is about 19 BITs, so we cannot sum the values with simple arithmetic operations. In the code, I use the sum of each character string. The main points in the Code are as follows:

1. How to obtain 100*50:

For an integer of 100 to 50 digits, read the text of each row to a two-dimensional array string [100] [51. Code uses the file descriptor of larger_sum.txt created by open, and uses creat to create a non-existing cc.txt text (used to determine whether the code can normally read 100 50-digit digits of the text ).

2. Algorithm for summation of 100 50 digits:

For the number of 100 digits and 50 digits, the Code uses a two-dimensional array to sum the number of 100 digits from the low position in sequence, for example, string [I] [0] + string [I] [1] +... + srting [I] [49] + business value, then perform the remainder operation and calculate the quotient. The remainder of each operation is stored in the int NewStr [51] integer array, and the Business Value of the last operation is kept in NewStr [50.

3. Compile Make file:

All: main. o

Gcc-o all main. c

Clean:

Rm all main. o

Note: 1. When the code is running, the warning: function returns address of local variable is displayed, which is translated as "warning: address returned by the local function ". This is because the code in the Cal_Digit function does not assign a fixed address to the returned pointer value, and the local variables in this function will automatically release the allocated memory after execution. As a result, the main function fails to be called. Use malloc to allocate fixed memory in the code, and then release the memory before the code is executed.

2. warning: incompatible implicit declaration of built-in function [enable by default]. This is used to call public functions without declaring the header files string. h and stdlib. h.

3. How to accurately calculate the execution time of a program in Linux. You can implement the following functions:

1) clock () function; its declaration is defined in the time. h header file.

2) time () function;

3) gettimeofday () function;

1 #include <string.h> 2 #include <stdlib.h> 3 #include <time.h> 4  5 int *Cal_Digit(char *string); 6 int main() 7 { 8     //read 100*50 digit into string 9     int fp, fp1;10     static int i = 0;11     static int j = 0;12     char string[100][51]= {0};13     int *result;14     clock_t time1,time2;15     time1 = clock();16     if((fp=open("//home//yb//test//Project_Euler//13.Larger_sum//Larger_sum.txt", 0, 0)) ==  -1)17     {18         printf("OPEN FILE ERROR\n");19     }20     creat("//home//yb//test//Project_Euler//13.Larger_sum//cc.txt");21     if((fp1=open("//home//yb//test//Project_Euler//13.Larger_sum//cc.txt", 2, 0)) ==  -1)22     {23         printf("OPEN FILE ERROR\n");24     }25     while( read(fp, string[i], 51) > 0)26     {27         write(fp1, string[i], 51);28         i++;29     }30     close(fp);31     close(fp1);32 33     //calculate 100*50 digit34     printf("Calcutate:\n");    35     result = Cal_Digit(&string[0][0]);36 37     //printf result38     for(i=50; i>=0;i--)39     {40         printf("%d", *(result+i));41     }42     free(result);43     time2 = clock();44     printf("\nRun Time:%fs \n", (double)(time2-time1)/CLOCKS_PER_SEC);45     return 0;46 }47 int *Cal_Digit(char *string)48 {49     int *NewStr;50     int i =0 , j=0;51     int z =0;52     int temp =0;53     int CarryBit = 0;54     NewStr = (int*)malloc(sizeof(int)*50);55 56     for(i=49; i>=0; i--)57     {58         temp = CarryBit;59         for(j=0; j<100; j++)60         {61             temp += *(string + j*51 + i) - 48;62         }63         NewStr[49-i]= temp%10;64         CarryBit = temp/10;65         if(i == 0)66         {67             NewStr[50] = CarryBit;68         }69     }70     return NewStr;71 }

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.