From b033b323fba368f7116d019186e23c88f59c78a7 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Thu, 2 Apr 2020 18:36:11 -0400 Subject: [PATCH 01/18] much faster Fibbonacci algorithm --- misc/factorial_fast.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 misc/factorial_fast.c diff --git a/misc/factorial_fast.c b/misc/factorial_fast.c new file mode 100644 index 00000000..2559aaca --- /dev/null +++ b/misc/factorial_fast.c @@ -0,0 +1,67 @@ +#include +#include +#include + +/** + Returns the \f$n^{th}\f$ and \f$n+1^{th}\f$ Fibonacci number. + The return variables are C & D respectively. + */ +void fib(unsigned long n, unsigned long *C, unsigned long *D) +{ + //Out of Range checking + if (n < 0) + { + printf("\nNo Such term !\n"); + exit(0); + } + + unsigned long a, b, c, d; + + if (n == 0) + { + C[0] = 0; + if (D) + D[0] = 1; + return; + } + + fib(n >> 1, &c, &d); /**< Compute F(n/2) */ + + a = c * ((d << 1) - c); + b = c * c + d * d; + if (n % 2 == 0) /**< If n is even */ + { + C[0] = a; + if (D) + D[0] = b; + return; + } + + /**< If n is odd */ + C[0] = b; + if (D) + D[0] = a + b; + return; +} + +int main(int argc, char *argv[]) +{ + unsigned long number, result; + + setlocale(LC_NUMERIC, ""); // format the printf output + + //Asks for the number/position of term in Fibonnacci sequence + if (argc == 2) + number = atoi(argv[1]); + else + { + printf("Enter the value of n(n starts from 0 ): "); + scanf("%lu", &number); + } + + fib(number, &result, NULL); + + printf("The nth term is : %'lu \n", result); + + return 0; +} From 74dfd1fffdc7772fc055d624f473c602ae44e6cd Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 3 Apr 2020 00:17:42 +0000 Subject: [PATCH 02/18] updating DIRECTORY.md --- DIRECTORY.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7167850a..2200cf13 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -182,6 +182,7 @@ * [Collatz](https://github.com/TheAlgorithms/C/blob/master/misc/Collatz.c) * [Demonetization](https://github.com/TheAlgorithms/C/blob/master/misc/demonetization.c) * [Factorial](https://github.com/TheAlgorithms/C/blob/master/misc/Factorial.c) + * [Factorial Fast](https://github.com/TheAlgorithms/C/blob/master/misc/factorial_fast.c) * [Factorial Trailing Zeroes](https://github.com/TheAlgorithms/C/blob/master/misc/factorial_trailing_zeroes.c) * [Fibonacci](https://github.com/TheAlgorithms/C/blob/master/misc/Fibonacci.c) * [Fibonacci Dp](https://github.com/TheAlgorithms/C/blob/master/misc/Fibonacci_DP.c) @@ -243,10 +244,10 @@ * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2015/sol1.c) * Problem 16 * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2016/sol1.c) - * Problem 20 - * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2020/sol1.c) * Problem 19 * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2019/sol1.c) + * Problem 20 + * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2020/sol1.c) * Problem 21 * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2021/sol1.c) * Problem 22 @@ -254,6 +255,8 @@ * Problem 23 * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2023/sol1.c) * [Sol2](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2023/sol2.c) + * Problem 25 + * [Sol1](https://github.com/TheAlgorithms/C/blob/master/project_euler/Problem%2025/sol1.c) ## Searching * [Binary Search](https://github.com/TheAlgorithms/C/blob/master/searching/Binary_Search.c) From c511a709cca15092447cc20d7acab0bdba818e96 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 6 Apr 2020 22:19:41 -0400 Subject: [PATCH 03/18] added submodule kvedala/function_primer --- .gitmodules | 3 +++ function_timer | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 function_timer diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..2b0b5bff --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "function_timer"] + path = function_timer + url = git@github.com:kvedala/function_timer.git diff --git a/function_timer b/function_timer new file mode 160000 index 00000000..0f62dda1 --- /dev/null +++ b/function_timer @@ -0,0 +1 @@ +Subproject commit 0f62dda134903687ebff6408a1495293e9e3f336 From d321ee09cc7bf98c9d5a0de4cfca29537ba0d1e9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 6 Apr 2020 22:22:54 -0400 Subject: [PATCH 04/18] updated submodule --- function_timer | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/function_timer b/function_timer index 0f62dda1..327ddab3 160000 --- a/function_timer +++ b/function_timer @@ -1 +1 @@ -Subproject commit 0f62dda134903687ebff6408a1495293e9e3f336 +Subproject commit 327ddab3e895c26026eeb39ed7e0b44d82597137 From 772dd98aa6cd58afd57fff117e1e3419086b594d Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 6 Apr 2020 22:38:33 -0400 Subject: [PATCH 05/18] do not track build folder --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8d3f5c3f..b4b13dd8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.exe *.out .vscode/ +build/ \ No newline at end of file From 45398453bee8deb4836c9346933a8499c6b01c33 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 6 Apr 2020 23:50:46 -0400 Subject: [PATCH 06/18] First working cmake --- CMakeLists.txt | 20 ++++++++++++++++++++ conversions/CMakeLists.txt | 15 +++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 conversions/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..f9557f12 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.0) +project(Algorithms_in_C + LANGUAGES C CXX + VERSION 1.0.0 + DESCRIPTION "Set of algorithms implemented in C." +) + +add_subdirectory(function_timer EXCLUDE_FROM_ALL) +include_directories(function_timer/include) +link_libraries(function_timer) +# include_directories(${CMAKE_BINARY_DIR}/include) +# link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) + +add_subdirectory(conversions) + +find_package(OpenMP) + +set(CPACK_PROJECT_NAME ${PROJECT_NAME}) +set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) +include(CPack) diff --git a/conversions/CMakeLists.txt b/conversions/CMakeLists.txt new file mode 100644 index 00000000..254a2f11 --- /dev/null +++ b/conversions/CMakeLists.txt @@ -0,0 +1,15 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".c" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + # Make sure YourLib is linked to each app + # target_link_libraries( ${testname} function_timer ) + install(TARGETS ${testname} DESTINATION "bin/conversions") + +endforeach( testsourcefile ${APP_SOURCES} ) From def308b78527a45421ecb932e64176596c94bebf Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Mon, 6 Apr 2020 23:59:19 -0400 Subject: [PATCH 07/18] remove conio.h --- misc/QUARTILE.c | 56 +++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/misc/QUARTILE.c b/misc/QUARTILE.c index a8498e64..f9ae5cc6 100644 --- a/misc/QUARTILE.c +++ b/misc/QUARTILE.c @@ -1,45 +1,47 @@ -#include -#include -#include -void main() +#include +#include +#include + +int main() { - int a[10],n,i,j,temp; - float q1,q3,iqr; - clrscr(); + int a[10], n, i, j, temp; + float q1, q3, iqr; + printf("Enter no. for Random Numbers :"); - scanf("%d",&n); - for(i=0;i Date: Mon, 6 Apr 2020 23:59:41 -0400 Subject: [PATCH 08/18] +misc/cmake --- misc/CMakeLists.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 misc/CMakeLists.txt diff --git a/misc/CMakeLists.txt b/misc/CMakeLists.txt new file mode 100644 index 00000000..be8a549f --- /dev/null +++ b/misc/CMakeLists.txt @@ -0,0 +1,16 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".c" "" testname ${testsourcefile} ) + add_executable( ${testname} ${testsourcefile} ) + # Make sure YourLib is linked to each app + target_link_libraries( ${testname} function_timer ) + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C) + install(TARGETS ${testname} DESTINATION "bin/misc") + +endforeach( testsourcefile ${APP_SOURCES} ) From 0c86721f4ad7f9bed7d3f55288b3d8d8d87cfcc9 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:00:04 -0400 Subject: [PATCH 09/18] better way to link libraries --- CMakeLists.txt | 3 ++- conversions/CMakeLists.txt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9557f12..702ef7e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,12 @@ project(Algorithms_in_C add_subdirectory(function_timer EXCLUDE_FROM_ALL) include_directories(function_timer/include) -link_libraries(function_timer) +# link_libraries(function_timer) # include_directories(${CMAKE_BINARY_DIR}/include) # link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) add_subdirectory(conversions) +add_subdirectory(misc) find_package(OpenMP) diff --git a/conversions/CMakeLists.txt b/conversions/CMakeLists.txt index 254a2f11..0a98602f 100644 --- a/conversions/CMakeLists.txt +++ b/conversions/CMakeLists.txt @@ -9,7 +9,8 @@ foreach( testsourcefile ${APP_SOURCES} ) string( REPLACE ".c" "" testname ${testsourcefile} ) add_executable( ${testname} ${testsourcefile} ) # Make sure YourLib is linked to each app - # target_link_libraries( ${testname} function_timer ) + target_link_libraries( ${testname} function_timer ) + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C) install(TARGETS ${testname} DESTINATION "bin/conversions") endforeach( testsourcefile ${APP_SOURCES} ) From 1e7fc84c3ab60270533072cb52c3e109f2a1477b Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:12:34 -0400 Subject: [PATCH 10/18] added cmake to project euler --- project_euler/CMakeLists.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 project_euler/CMakeLists.txt diff --git a/project_euler/CMakeLists.txt b/project_euler/CMakeLists.txt new file mode 100644 index 00000000..234069e8 --- /dev/null +++ b/project_euler/CMakeLists.txt @@ -0,0 +1,22 @@ +# If necessary, use the RELATIVE flag, otherwise each source file may be listed +# with full pathname. RELATIVE may makes it easier to extract an executable name +# automatically. +file( GLOB_RECURSE APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c ) +# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) +# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) +foreach( testsourcefile ${APP_SOURCES} ) + # I used a simple string replace, to cut off .cpp. + string( REPLACE ".c" "" testname ${testsourcefile} ) + string( REPLACE "/" "-" testname ${testname} ) + string( REPLACE "\\" "-" testname ${testname} ) + string( REPLACE " " "_" testname ${testname} ) + add_executable( ${testname} ${testsourcefile} ) + # Make sure YourLib is linked to each app + target_link_libraries( ${testname} function_timer ) + if(OpenMP_C_FOUND) + target_link_libraries(${testname} OpenMP::OpenMP_C) + endif() + set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE C) + install(TARGETS ${testname} DESTINATION "bin/misc") + +endforeach( testsourcefile ${APP_SOURCES} ) From d24b6ea344fdac918db953dfc012d5d6df0d44bb Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:24:13 -0400 Subject: [PATCH 11/18] added option to enable or disable use of openmp --- CMakeLists.txt | 7 ++++++- project_euler/CMakeLists.txt | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 702ef7e7..6c8b0795 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,8 @@ project(Algorithms_in_C DESCRIPTION "Set of algorithms implemented in C." ) +option(USE_OPENMP "flag to use OpenMP for multithreading" ON) + add_subdirectory(function_timer EXCLUDE_FROM_ALL) include_directories(function_timer/include) # link_libraries(function_timer) @@ -13,8 +15,11 @@ include_directories(function_timer/include) add_subdirectory(conversions) add_subdirectory(misc) +add_subdirectory(project_euler) -find_package(OpenMP) +if(USE_OPENMP) + find_package(OpenMP) +endif() set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) diff --git a/project_euler/CMakeLists.txt b/project_euler/CMakeLists.txt index 234069e8..abb40df5 100644 --- a/project_euler/CMakeLists.txt +++ b/project_euler/CMakeLists.txt @@ -1,3 +1,7 @@ +if(USE_OPENMP) + find_package(OpenMP) +endif() + # If necessary, use the RELATIVE flag, otherwise each source file may be listed # with full pathname. RELATIVE may makes it easier to extract an executable name # automatically. From 2d9d2d87fe701a5e9a24431282c99fd664f86fc3 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:24:40 -0400 Subject: [PATCH 12/18] using the new function_timer library --- project_euler/Problem 23/sol1.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/project_euler/Problem 23/sol1.c b/project_euler/Problem 23/sol1.c index e7145946..b72e132b 100644 --- a/project_euler/Problem 23/sol1.c +++ b/project_euler/Problem 23/sol1.c @@ -1,10 +1,10 @@ - #include #include #include #ifdef _OPENMP #include #endif +#include "function_timer.h" unsigned long MAX_N = 28123; @@ -91,26 +91,28 @@ int main(int argc, char **argv) printf("Not using parallleization!\n"); #endif - clock_t dt = 0; + double total_duration = 0; + function_timer *timer = new_timer(); #ifdef _OPENMP #pragma omp parallel for reduction(+ \ : sum) schedule(runtime) #endif for (unsigned long i = 1; i <= MAX_N; i++) { - clock_t start_time = clock(); + start_timer(timer); if (!is_sum_of_abundant(i)) sum += i; clock_t end_time = clock(); - dt += end_time - start_time; + total_duration += end_timer(timer); printf("... %5lu: %8lu\r", i, sum); if (i % 100 == 0) fflush(stdout); } - printf("Time taken: %.4g ms\n", 1e3 * dt / CLOCKS_PER_SEC); + printf("Time taken: %.4g s\n", total_duration); printf("Sum of numbers that cannot be represented as sum of two abundant numbers : %lu\n", sum); + delete_timer(timer); return 0; } From 682b48b83a034767eca963bb8a413d3096489df1 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:28:15 -0400 Subject: [PATCH 13/18] added travis CI --- .travis.yml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..1664fe08 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,27 @@ +language: cpp +os: linux +compiler: + - gcc + - clang + +before_install: + - test -n $CC && unset CC + +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - libgomp1 + # - gcc-6 + +script: + - export OMP_NUM_THREADS=2 + - export LD_LIBRARY_PATH=$(if [[ $CXX == "clang++" ]]; then echo -n '/usr/local/clang/lib'; fi) + - mkdir build + - cd build + - cmake .. -DUSE_OPENMP=OFF + - make + - rm -rf * + - cmake .. -DUSE_OPENMP=ON + - make From 35f897352e6f93e86f93c8324eb6fb7c0681babf Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:33:46 -0400 Subject: [PATCH 14/18] use https for submodule --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 2b0b5bff..949b4bc7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "function_timer"] path = function_timer - url = git@github.com:kvedala/function_timer.git + url = https://github.com/kvedala/function_timer.git From fe5c6a724d82fd4c7ebbde3efb56ebf3201afbdb Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:37:24 -0400 Subject: [PATCH 15/18] replace uint8 with unsigned char --- project_euler/Problem 25/sol1.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/project_euler/Problem 25/sol1.c b/project_euler/Problem 25/sol1.c index b6addd95..7d189e53 100644 --- a/project_euler/Problem 25/sol1.c +++ b/project_euler/Problem 25/sol1.c @@ -9,9 +9,9 @@ * Function to add arbitraty length decimal integers stored in an array. * a + b = c = new b **/ -unsigned int add_numbers(uint8_t *a, uint8_t *b, uint8_t *c, int N) +unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c, int N) { - uint8_t carry = 0; + unsigned char carry = 0; unsigned int i; for (i = 0; i < N; i++) @@ -46,7 +46,7 @@ unsigned int add_numbers(uint8_t *a, uint8_t *b, uint8_t *c, int N) return i; } -int print_number(uint8_t *number, int N) +int print_number(unsigned char *number, int N) { int start_pos = N - 1; @@ -60,7 +60,7 @@ int print_number(uint8_t *number, int N) return 0; } -unsigned int get_digits(uint8_t *number) +unsigned int get_digits(unsigned char *number) { unsigned int digits = MAX_DIGITS; while (number[digits] == 0) @@ -70,9 +70,9 @@ unsigned int get_digits(uint8_t *number) int main(int argc, char *argv[]) { - uint8_t fn[MAX_DIGITS + 1]; /* array to store digits of a large number */ - uint8_t fn1[MAX_DIGITS + 1]; - uint8_t sum[MAX_DIGITS + 1]; + unsigned char fn[MAX_DIGITS + 1]; /* array to store digits of a large number */ + unsigned char fn1[MAX_DIGITS + 1]; + unsigned char sum[MAX_DIGITS + 1]; memset(fn, 0, MAX_DIGITS); memset(fn1, 0, MAX_DIGITS); From 6a09ade47d262887cbf143c2b3bdde751be4879a Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:39:42 -0400 Subject: [PATCH 16/18] added stdint.h for fixed width ints --- project_euler/Problem 08/sol1.c | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/project_euler/Problem 08/sol1.c b/project_euler/Problem 08/sol1.c index 3577e3a0..1c57045e 100644 --- a/project_euler/Problem 08/sol1.c +++ b/project_euler/Problem 08/sol1.c @@ -1,20 +1,21 @@ #include +#include #include int64_t get_product(FILE *fp, long start_pos, int num_digits) { - char ch = ' '; /* temporary variable to store character read from file */ - uint8_t num = 0; /* temporary variable to store digit read */ - int64_t prod = 1; /* product accumulator */ - int count = 0; /* we use this variable to count number of bytes of file read */ + char ch = ' '; /* temporary variable to store character read from file */ + uint8_t num = 0; /* temporary variable to store digit read */ + int64_t prod = 1; /* product accumulator */ + int count = 0; /* we use this variable to count number of bytes of file read */ /* accumulate product for num_digits */ - for(int i = 0; i < num_digits; i++, count++) + for (int i = 0; i < num_digits; i++, count++) { /* get character from file */ ch = getc(fp); - /* the ASCII codes of digits is between 0x30 and 0x39. + /* the ASCII codes of digits is between 0x30 and 0x39. * any character not in this range implies an invalid character */ if (ch < 0x30 || ch > 0x39) @@ -24,35 +25,34 @@ int64_t get_product(FILE *fp, long start_pos, int num_digits) i--; continue; } - + num = ch - 0x30; /* convert character digit to number */ if (num == 0) { - /* If number is zero, we can skip the next 'num_digits' + /* If number is zero, we can skip the next 'num_digits' * because this '0' will repeat in the next 'num_digit' multiplications. * Hence, we also do not update the file position */ /* NOTE: this is not needed but helps get results faster :) */ return 0; } - prod *= num; /* accumulate product */ + prod *= num; /* accumulate product */ } /* set file position to the next starting character + 1 */ fseek(fp, -count + 1, SEEK_CUR); - + return prod; } - -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { int position = 0; int num_digits = 4; int64_t prod, max_prod = 0; /* if second command-line argument is ge=iven, - * use it as the number of digits to compute + * use it as the number of digits to compute * successive product for */ if (argc == 2) @@ -67,24 +67,24 @@ int main(int argc, char* argv[]) } /* loop through all digits in the file */ - do + do { /* get product of 'num_digits' from current position in file */ prod = get_product(fp, ftell(fp), num_digits); - + if (prod > max_prod) { max_prod = prod; position = ftell(fp) - 1; } - } while(!feof(fp)); /* loop till end of file is reached */ - + } while (!feof(fp)); /* loop till end of file is reached */ + printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod, position); - fseek(fp, position, SEEK_SET); /* move cursor to identified position in file */ + fseek(fp, position, SEEK_SET); /* move cursor to identified position in file */ /* loop through all digits */ for (; num_digits > 0; num_digits--) { - char ch = getc(fp); /* get character */ + char ch = getc(fp); /* get character */ /* skip invalid character */ if (ch < 0x30 || ch > 0x39) continue; @@ -94,7 +94,7 @@ int main(int argc, char* argv[]) printf("%c = %lld\n", ch, max_prod); } - fclose(fp); /* close file */ + fclose(fp); /* close file */ return 0; } From 860b1fd5010f6d2823b618b22e562cc1ed2ad172 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 7 Apr 2020 00:41:14 -0400 Subject: [PATCH 17/18] added stdint.h for fixed width ints --- project_euler/Problem 08/sol2.c | 56 +++++++++++++++++---------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/project_euler/Problem 08/sol2.c b/project_euler/Problem 08/sol2.c index be31b11c..67371886 100644 --- a/project_euler/Problem 08/sol2.c +++ b/project_euler/Problem 08/sol2.c @@ -1,8 +1,9 @@ #include #include -#include /* for memmove */ +#include +#include /* for memmove */ -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { int position = 0, num_bad_chars = 0; int num_digits = 4; @@ -12,15 +13,15 @@ int main(int argc, char* argv[]) int64_t prod = 1, max_prod = 0; /* if second command-line argument is given, - * use it as the number of digits to compute + * use it as the number of digits to compute * successive product for */ if (argc == 2) num_digits = atoi(argv[1]); - + /* allocate memory to store past values */ buffer = calloc(num_digits, sizeof(uint8_t)); - if(!buffer) + if (!buffer) { perror("Unable to allocate memory for buffer"); return -1; @@ -31,57 +32,58 @@ int main(int argc, char* argv[]) if (!fp) { perror("Unable to open file"); - free(buffer); /* free allocated memory */ + free(buffer); /* free allocated memory */ return -1; } /* loop through all digits in the file */ - do + do { /* get character from file */ ch = getc(fp); - /* the ASCII codes of digits is between 0x30 and 0x39. + /* the ASCII codes of digits is between 0x30 and 0x39. * any character not in this range implies an invalid character */ if (ch < 0x30 || ch > 0x39) { - num_bad_chars ++; /* this is used to get the bad characters in the sequence of 13 characters */ + num_bad_chars++; /* this is used to get the bad characters in the sequence of 13 characters */ continue; - } else if (num_bad_chars > 0) - num_bad_chars --; - - num = ch - 0x30; /* convert character digit to number */ - num_prev = buffer[0]; /* previous n^th digit */ + } + else if (num_bad_chars > 0) + num_bad_chars--; + + num = ch - 0x30; /* convert character digit to number */ + num_prev = buffer[0]; /* previous n^th digit */ /* left shift the buffer - * using a for loop or a faster memory move */ - memmove(buffer, buffer+1, num_digits-1); + memmove(buffer, buffer + 1, num_digits - 1); /* for (int i = 1; i < num_digits; i++) buffer[i-1] = buffer[i]; */ - buffer[num_digits-1] = num; /* save the latest number in buffer */ + buffer[num_digits - 1] = num; /* save the latest number in buffer */ if (num_prev != 0) { - /* since product is accumulated, the new product can be obtained by simply + /* since product is accumulated, the new product can be obtained by simply * multiplying the new digit and dividing with the oldest digit */ - prod /= num_prev; /* divide first to avoid over-flows */ + prod /= num_prev; /* divide first to avoid over-flows */ prod *= num; } else { prod = 1; - for(int i = 0; i < num_digits; i++) + for (int i = 0; i < num_digits; i++) { - if(buffer[i] == 0) + if (buffer[i] == 0) { prod = 0; - break; /* break innermost for-loop */ + break; /* break innermost for-loop */ } prod *= buffer[i]; } @@ -93,14 +95,14 @@ int main(int argc, char* argv[]) max_prod = prod; position = ftell(fp) - num_bad_chars - num_digits - 1; } - } while(!feof(fp)); /* loop till end of file is reached */ - + } while (!feof(fp)); /* loop till end of file is reached */ + printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod, position); - fseek(fp, position, SEEK_SET); /* move cursor to identified position in file */ + fseek(fp, position, SEEK_SET); /* move cursor to identified position in file */ /* loop through all digits */ for (; num_digits > 0; num_digits--) { - char ch = getc(fp); /* get character */ + char ch = getc(fp); /* get character */ /* skip invalid character */ if (ch < 0x30 || ch > 0x39) continue; @@ -110,8 +112,8 @@ int main(int argc, char* argv[]) printf("%c = %lld\n", ch, max_prod); } - fclose(fp); /* close file */ - free(buffer); /* free allocated memory */ + fclose(fp); /* close file */ + free(buffer); /* free allocated memory */ return 0; } From 3611c5e8f486099061b81a66264d2262df8a0e89 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Tue, 7 Apr 2020 00:45:55 -0400 Subject: [PATCH 18/18] added build status badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 912c5e2f..f397984c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Build Status](https://travis-ci.org/kvedala/C.svg?branch=master)](https://travis-ci.org/kvedala/C) C ========