diff --git a/machine_learning/adaline_learning.c b/machine_learning/adaline_learning.c index 3bac6823..ecef67b1 100644 --- a/machine_learning/adaline_learning.c +++ b/machine_learning/adaline_learning.c @@ -3,6 +3,8 @@ * \brief [Adaptive Linear Neuron * (ADALINE)](https://en.wikipedia.org/wiki/ADALINE) implementation * + * \author [Krishna Vedala](https://github.com/kvedala) + * * diff --git a/machine_learning/kohonen_som_trace.c b/machine_learning/kohonen_som_trace.c index ec894c44..4c313f77 100644 --- a/machine_learning/kohonen_som_trace.c +++ b/machine_learning/kohonen_som_trace.c @@ -3,6 +3,8 @@ * \brief [Kohonen self organizing * map](https://en.wikipedia.org/wiki/Self-organizing_map) (data tracing) * + * \author [Krishna Vedala](https://github.com/kvedala) + * * This example implements a powerful self organizing map algorithm. * The algorithm creates a connected network of weights that closely * follows the given data points. This this creates a chain of nodes that diff --git a/misc/collatz.c b/misc/collatz.c index a99140ea..0ba7a0da 100644 --- a/misc/collatz.c +++ b/misc/collatz.c @@ -1,11 +1,18 @@ -/* -collatz conjecture: a series for a number n in which if n even then the next -number is n/2 ,but if n is odd then the next number is 3n+1. this series -continues till it reaches 1*/ +/** + * \file + * + * \brief Implementation of [Collatz' + * conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) + * + * Collatz conjecture: a series for a number \f$n\f$ in which if \f$n\f$ even + * then the next number is \f$\frac{n}{2}\f$ ,but if n is odd then the next + * number is \f$3n+1\f$. This series continues till \f$n\f$ reaches 1 + */ #include #include +/** Main function */ int main(int argc, char *argv[]) { unsigned long long n, curr_no, num_steps = 0; diff --git a/misc/factorial_large_number.c b/misc/factorial_large_number.c index 2a77ddc2..9720d709 100644 --- a/misc/factorial_large_number.c +++ b/misc/factorial_large_number.c @@ -2,6 +2,7 @@ * @file * \brief Compute factorial of arbitrarily large numbers by * storing individual digits in a byte. + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/misc/fibonacci_fast.c b/misc/fibonacci_fast.c index 653ecfd4..e12a3b98 100644 --- a/misc/fibonacci_fast.c +++ b/misc/fibonacci_fast.c @@ -1,6 +1,6 @@ /** @file - @author Krishna Vedala + @author [Krishna Vedala](https://github.com/kvedala) @date 2 October, 2019 @brief Compute \f$m^{mth}\f$ Fibonacci number using the formulae: \f{eqnarray*}{ diff --git a/numerical_methods/durand_kerner_roots.c b/numerical_methods/durand_kerner_roots.c index 443b8610..440fad1c 100644 --- a/numerical_methods/durand_kerner_roots.c +++ b/numerical_methods/durand_kerner_roots.c @@ -4,6 +4,8 @@ * [Durand Kerner * algorithm](https://en.wikipedia.org/wiki/Durand%E2%80%93Kerner_method) * + * \author [Krishna Vedala](https://github.com/kvedala) + * * Test the algorithm online: * https://gist.github.com/kvedala/27f1b0b6502af935f6917673ec43bcd7 * @@ -249,4 +251,4 @@ end: free(s0); return 0; -} \ No newline at end of file +} diff --git a/numerical_methods/newton_raphson_root.c b/numerical_methods/newton_raphson_root.c index e699b80b..7f071db9 100644 --- a/numerical_methods/newton_raphson_root.c +++ b/numerical_methods/newton_raphson_root.c @@ -2,6 +2,8 @@ * @file * \brief Find approximate solution for \f$f(x) = 0\f$ using * Newton-Raphson interpolation algorithm. + * + * \author [Krishna Vedala](https://github.com/kvedala) **/ #include /* requires minimum of C99 */ @@ -71,4 +73,4 @@ int main(int argc, char **argv) c >= 0 ? '+' : '-', c >= 0 ? c : -c, delta); return 0; -} \ No newline at end of file +} diff --git a/numerical_methods/qr_decompose.h b/numerical_methods/qr_decompose.h index 4404b0b1..73513a39 100644 --- a/numerical_methods/qr_decompose.h +++ b/numerical_methods/qr_decompose.h @@ -1,9 +1,9 @@ /** * @file - * * \brief Library functions to compute [QR * decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given * matrix. + * \author [Krishna Vedala](https://github.com/kvedala) */ #ifndef QR_DECOMPOSE_H diff --git a/numerical_methods/qr_decomposition.c b/numerical_methods/qr_decomposition.c index c758aaa0..f953b96b 100644 --- a/numerical_methods/qr_decomposition.c +++ b/numerical_methods/qr_decomposition.c @@ -3,6 +3,7 @@ * \brief Program to compute the [QR * decomposition](https://en.wikipedia.org/wiki/QR_decomposition) of a given * matrix. + * \author [Krishna Vedala](https://github.com/kvedala) */ #include "qr_decompose.h" @@ -76,4 +77,4 @@ int main(void) free(R); free(Q); return 0; -} \ No newline at end of file +} diff --git a/numerical_methods/qr_eigen_values.c b/numerical_methods/qr_eigen_values.c index cfbb961c..5028b848 100644 --- a/numerical_methods/qr_eigen_values.c +++ b/numerical_methods/qr_eigen_values.c @@ -3,6 +3,7 @@ * \brief Compute real eigen values and eigen vectors of a symmetric matrix * using [QR decomposition](https://en.wikipedia.org/wiki/QR_decomposition) * method. + * \author [Krishna Vedala](https://github.com/kvedala) */ #include "qr_decompose.h" #include @@ -170,4 +171,4 @@ int main(int argc, char **argv) free(Q); free(eigen_vals); return 0; -} \ No newline at end of file +} diff --git a/numerical_methods/realtime_stats.c b/numerical_methods/realtime_stats.c index d2bac7a2..83064821 100644 --- a/numerical_methods/realtime_stats.c +++ b/numerical_methods/realtime_stats.c @@ -1,6 +1,7 @@ /** * \file * \brief Compute statistics for data entered in rreal-time + * \author [Krishna Vedala](https://github.com/kvedala) * * This algorithm is really beneficial to compute statistics on data read in * realtime. For example, devices reading biometrics data. The algorithm is diff --git a/project_euler/problem_10/sol1.c b/project_euler/problem_10/sol1.c index 8e6976a2..560582bc 100644 --- a/project_euler/problem_10/sol1.c +++ b/project_euler/problem_10/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 10](https://projecteuler.net/problem=10) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_10/sol2.c b/project_euler/problem_10/sol2.c index 6926ae06..6c0cda64 100644 --- a/project_euler/problem_10/sol2.c +++ b/project_euler/problem_10/sol2.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 10](https://projecteuler.net/problem=10) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include @@ -52,4 +53,4 @@ int main(int argc, char *argv[]) printf("%ld: %lld\n", n, sum); return 0; -} \ No newline at end of file +} diff --git a/project_euler/problem_12/sol1.c b/project_euler/problem_12/sol1.c index 46c0162b..d00814ac 100644 --- a/project_euler/problem_12/sol1.c +++ b/project_euler/problem_12/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 12](https://projecteuler.net/problem=12) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_13/sol1.c b/project_euler/problem_13/sol1.c index 2ee588c1..c1d05327 100644 --- a/project_euler/problem_13/sol1.c +++ b/project_euler/problem_13/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 13](https://projecteuler.net/problem=13) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_14/sol1.c b/project_euler/problem_14/sol1.c index 3190aabd..cfb2422f 100644 --- a/project_euler/problem_14/sol1.c +++ b/project_euler/problem_14/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 14](https://projecteuler.net/problem=14) solution + * \author [Krishna Vedala](https://github.com/kvedala) * * Since the computational values for each iteration step are independent, * we can compute them in parallel. However, the maximum values should be diff --git a/project_euler/problem_15/sol1.c b/project_euler/problem_15/sol1.c index 249fc124..c0dd1c79 100644 --- a/project_euler/problem_15/sol1.c +++ b/project_euler/problem_15/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 15](https://projecteuler.net/problem=15) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_16/sol1.c b/project_euler/problem_16/sol1.c index 91a38fbe..68a12cac 100644 --- a/project_euler/problem_16/sol1.c +++ b/project_euler/problem_16/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 16](https://projecteuler.net/problem=16) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_19/sol1.c b/project_euler/problem_19/sol1.c index fee6ebd7..4e91ee25 100644 --- a/project_euler/problem_19/sol1.c +++ b/project_euler/problem_19/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 19](https://projecteuler.net/problem=19) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include diff --git a/project_euler/problem_20/sol1.c b/project_euler/problem_20/sol1.c index 9cd4cb79..1c6e9e1f 100644 --- a/project_euler/problem_20/sol1.c +++ b/project_euler/problem_20/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 20](https://projecteuler.net/problem=20) solution + * \author [Krishna Vedala](https://github.com/kvedala) * * Implementation uses a custom `big_int` structure that can store arbitrarilty * large integer numbers. diff --git a/project_euler/problem_21/sol1.c b/project_euler/problem_21/sol1.c index 90521fdc..d5b77145 100644 --- a/project_euler/problem_21/sol1.c +++ b/project_euler/problem_21/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 21](https://projecteuler.net/problem=21) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_22/sol1.c b/project_euler/problem_22/sol1.c index 6a8e7a42..bd8aee8d 100644 --- a/project_euler/problem_22/sol1.c +++ b/project_euler/problem_22/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 22](https://projecteuler.net/problem=22) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_23/sol1.c b/project_euler/problem_23/sol1.c index f88882c0..308b0362 100644 --- a/project_euler/problem_23/sol1.c +++ b/project_euler/problem_23/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 23](https://projecteuler.net/problem=23) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_23/sol2.c b/project_euler/problem_23/sol2.c index 44c00aec..6041d0a0 100644 --- a/project_euler/problem_23/sol2.c +++ b/project_euler/problem_23/sol2.c @@ -2,6 +2,7 @@ * \file * \brief [Problem 23](https://projecteuler.net/problem=23) solution - * optimization using look-up array + * \author [Krishna Vedala](https://github.com/kvedala) * * Optimization applied - compute & store abundant numbers once * into a look-up array. diff --git a/project_euler/problem_25/sol1.c b/project_euler/problem_25/sol1.c index 44a33d9d..e5bef0bc 100644 --- a/project_euler/problem_25/sol1.c +++ b/project_euler/problem_25/sol1.c @@ -2,6 +2,7 @@ * \file * \brief [Problem 25](https://projecteuler.net/problem=25) solution implemented * using arbitrarily large numbers represented as arrays + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_26/sol1.c b/project_euler/problem_26/sol1.c index 6ddc1a80..b5359ed2 100644 --- a/project_euler/problem_26/sol1.c +++ b/project_euler/problem_26/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 26](https://projecteuler.net/problem=26) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_401/sol1.c b/project_euler/problem_401/sol1.c index 43eee4f0..ed260b32 100644 --- a/project_euler/problem_401/sol1.c +++ b/project_euler/problem_401/sol1.c @@ -1,8 +1,8 @@ /** * \file - * \brief [Problem 401](https://projecteuler.net/problem=401) solution - * + * \brief [Problem 401](https://projecteuler.net/problem=401) solution - * Sum of squares of divisors + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include @@ -144,4 +144,4 @@ int main(int argc, char **argv) printf("Time taken: %.4gms\n", dtime * 1e3 / CLOCKS_PER_SEC); return 0; -} \ No newline at end of file +} diff --git a/project_euler/problem_8/sol1.c b/project_euler/problem_8/sol1.c index 4b18a60a..01259489 100644 --- a/project_euler/problem_8/sol1.c +++ b/project_euler/problem_8/sol1.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 8](https://projecteuler.net/problem=8) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_8/sol2.c b/project_euler/problem_8/sol2.c index fda6ab0c..46569119 100644 --- a/project_euler/problem_8/sol2.c +++ b/project_euler/problem_8/sol2.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 8](https://projecteuler.net/problem=8) solution + * \author [Krishna Vedala](https://github.com/kvedala) */ #include #include diff --git a/project_euler/problem_9/sol1.c b/project_euler/problem_9/sol1.c index 6a0a64b8..cba1a89a 100644 --- a/project_euler/problem_9/sol1.c +++ b/project_euler/problem_9/sol1.c @@ -2,6 +2,7 @@ * \file * \brief [Problem 9](https://projecteuler.net/problem=9) solution - A naive * implementation + * \author [Krishna Vedala](https://github.com/kvedala) */ #include @@ -22,4 +23,4 @@ int main(void) } return 0; -} \ No newline at end of file +} diff --git a/project_euler/problem_9/sol2.c b/project_euler/problem_9/sol2.c index 69153b3e..4afe69d5 100644 --- a/project_euler/problem_9/sol2.c +++ b/project_euler/problem_9/sol2.c @@ -1,6 +1,7 @@ /** * \file * \brief [Problem 9](https://projecteuler.net/problem=9) solution + * \author [Krishna Vedala](https://github.com/kvedala) * Problem Statement: A Pythagorean triplet is a set of three natural numbers, \f$a < b < c\f$, diff --git a/sorting/shell_sort2.c b/sorting/shell_sort2.c index 7802edf1..c6ae4851 100644 --- a/sorting/shell_sort2.c +++ b/sorting/shell_sort2.c @@ -1,20 +1,25 @@ +/** + * \file + * \brief [Shell sort algorithm](https://en.wikipedia.org/wiki/Shell_sort) + * implementation. + * \author [Krishna Vedala](https://github.com/kvedala) + */ #include #include #include -#define ELEMENT_NR 20000 #define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0])) -void show_data(int arr[], int len) +/** Helper function to print array values */ +void show_data(int *arr, long len) { - int i; - - for (i = 0; i < len; i++) + for (long i = 0; i < len; i++) printf("%3d ", arr[i]); printf("\n"); } -void swap(int *a, int *b) +/** Function to swap values of two integers */ +inline void swap(int *a, int *b) { int tmp; @@ -26,17 +31,17 @@ void swap(int *a, int *b) /** * Optimized algorithm - takes half the time as other **/ -void shell_sort(int array[], int LEN) +void shell_sort(int *array, long LEN) { const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1}; const int gap_len = 8; - int i, j, g; + long i, j, g; for (g = 0; g < gap_len; g++) - { + { // for each gap int gap = gaps[g]; for (i = gap; i < LEN; i++) - { + { // from gap position to the end int tmp = array[i]; for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap) @@ -50,27 +55,32 @@ void shell_sort(int array[], int LEN) #endif } +/** Main function */ int main(int argc, char *argv[]) { int i; - int array[ELEMENT_NR]; - int range = 500; - int size; + long size = 500; + if (argc == 2) + size = atol(argv[1]); + else if (argc > 2) + fprintf(stderr, "Usage: ./shell_sort [number of values]\n"); + + int *array = (int *)malloc(size * sizeof(int)); + int range = 500; // range of array values double time_spent; - srand(time(NULL)); - for (i = 0; i < ELEMENT_NR; i++) + srand(time(NULL)); // initialize random number generator + for (i = 0; i < size; i++) + // fill array with random integers array[i] = rand() % range + 1; - size = ARRAY_LEN(array); - - show_data(array, size); - clock_t t1 = clock(); - shell_sort(array, size); - clock_t t2 = clock(); + show_data(array, size); // show array before sorting + clock_t t1 = clock(); // start timer + shell_sort(array, size); // sort the array + clock_t t2 = clock(); // end timer printf("Data Sorted\n"); - show_data(array, size); + show_data(array, size); // display array after sorting printf("Time spent sorting: %.4g s\n", (t2 - t1) / CLOCKS_PER_SEC);