mirror of https://github.com/TheAlgorithms/C
fixed documentations
This commit is contained in:
parent
aa8d74543f
commit
6470f0318b
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Node, the basic data structure of the tree
|
* Node, the basic data structure of the tree
|
||||||
**/
|
*/
|
||||||
typedef struct Node
|
typedef struct Node
|
||||||
{
|
{
|
||||||
int data; /**< stores the number */
|
int data; /**< stores the number */
|
||||||
|
@ -34,7 +34,7 @@ typedef struct Node
|
||||||
* creates a new node
|
* creates a new node
|
||||||
* param[in] data value to be inserted
|
* param[in] data value to be inserted
|
||||||
* \returns a pointer to the new node
|
* \returns a pointer to the new node
|
||||||
**/
|
*/
|
||||||
node *create_node(int data)
|
node *create_node(int data)
|
||||||
{
|
{
|
||||||
node *ptr = (node *)malloc(sizeof(node));
|
node *ptr = (node *)malloc(sizeof(node));
|
||||||
|
|
|
@ -30,17 +30,18 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn
|
/** Maximum number of iterations to learn */
|
||||||
|
#define MAX_ITER 500 // INT_MAX
|
||||||
|
|
||||||
/** structure to hold adaline model parameters */
|
/** structure to hold adaline model parameters */
|
||||||
struct adaline
|
struct adaline
|
||||||
{
|
{
|
||||||
double eta; ///< learning rate of the algorithm
|
double eta; /**< learning rate of the algorithm */
|
||||||
double *weights; ///< weights of the neural network
|
double *weights; /**< weights of the neural network */
|
||||||
int num_weights; ///< number of weights of the neural network
|
int num_weights; /**< number of weights of the neural network */
|
||||||
};
|
};
|
||||||
|
|
||||||
#define ACCURACY 1e-5 ///< convergence accuracy \f$=1\times10^{-5}\f$
|
#define ACCURACY 1e-5 /**< convergence accuracy \f$=1\times10^{-5}\f$ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor
|
* Default constructor
|
||||||
|
|
|
@ -28,14 +28,12 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef max
|
#ifndef max
|
||||||
#define max(a, b) \
|
/** shorthand for maximum value */
|
||||||
(((a) > (b)) ? (a) : (b)) /**< shorthand for maximum value \
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||||
*/
|
|
||||||
#endif
|
#endif
|
||||||
#ifndef min
|
#ifndef min
|
||||||
#define min(a, b) \
|
/** shorthand for minimum value */
|
||||||
(((a) < (b)) ? (a) : (b)) /**< shorthand for minimum value \
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
||||||
*/
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** to store info regarding 3D arrays */
|
/** to store info regarding 3D arrays */
|
||||||
|
@ -193,8 +191,8 @@ int save_u_matrix(const char *fname, struct array_3d *W)
|
||||||
* \param[in] X matrix to search
|
* \param[in] X matrix to search
|
||||||
* \param[in] N number of points in the vector
|
* \param[in] N number of points in the vector
|
||||||
* \param[out] val minimum value found
|
* \param[out] val minimum value found
|
||||||
* \param[out] idx_x x-index where minimum value was found
|
* \param[out] x_idx x-index where minimum value was found
|
||||||
* \param[out] idx_y y-index where minimum value was found
|
* \param[out] y_idx y-index where minimum value was found
|
||||||
*/
|
*/
|
||||||
void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)
|
void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)
|
||||||
{
|
{
|
||||||
|
@ -300,7 +298,6 @@ double update_weights(const double *X, struct array_3d *W, double **D,
|
||||||
*
|
*
|
||||||
* \param[in] X data set
|
* \param[in] X data set
|
||||||
* \param[in,out] W weights matrix
|
* \param[in,out] W weights matrix
|
||||||
* \param[in] D temporary vector to store distances
|
|
||||||
* \param[in] num_samples number of output points
|
* \param[in] num_samples number of output points
|
||||||
* \param[in] num_features number of features per input sample
|
* \param[in] num_features number of features per input sample
|
||||||
* \param[in] num_out number of output points
|
* \param[in] num_out number of output points
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*
|
*
|
||||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||||
*
|
*
|
||||||
|
* \details
|
||||||
* This example implements a powerful self organizing map algorithm.
|
* This example implements a powerful self organizing map algorithm.
|
||||||
* The algorithm creates a connected network of weights that closely
|
* The algorithm creates a connected network of weights that closely
|
||||||
* follows the given data points. This this creates a chain of nodes that
|
* follows the given data points. This this creates a chain of nodes that
|
||||||
|
@ -16,23 +17,22 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#ifdef _OPENMP // check if OpenMP based parallellization is available
|
#ifdef _OPENMP // check if OpenMP based parallelization is available
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef max
|
#ifndef max
|
||||||
#define max(a, b) \
|
/** shorthand for maximum value */
|
||||||
(((a) > (b)) ? (a) : (b)) /**< shorthand for maximum value \
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||||
*/
|
|
||||||
#endif
|
#endif
|
||||||
#ifndef min
|
#ifndef min
|
||||||
#define min(a, b) \
|
/** shorthand for minimum value */
|
||||||
(((a) < (b)) ? (a) : (b)) /**< shorthand for minimum value \
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
||||||
*/
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to generate a random number in a given interval.
|
* \brief Helper function to generate a random number in a given interval.
|
||||||
|
* \details
|
||||||
* \n Steps:
|
* \n Steps:
|
||||||
* 1. `r1 = rand() % 100` gets a random number between 0 and 99
|
* 1. `r1 = rand() % 100` gets a random number between 0 and 99
|
||||||
* 2. `r2 = r1 / 100` converts random number to be between 0 and 0.99
|
* 2. `r2 = r1 / 100` converts random number to be between 0 and 0.99
|
||||||
|
@ -41,22 +41,23 @@
|
||||||
* y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \;
|
* y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \;
|
||||||
* \text{mod}\; 100}{100} + a \f]
|
* \text{mod}\; 100}{100} + a \f]
|
||||||
*
|
*
|
||||||
* \param[in] a lower limit
|
* \param a lower limit
|
||||||
* \param[in] b upper limit
|
* \param b upper limit
|
||||||
* \returns random number in the range \f$[a,b)\f$
|
* \returns random number in the range \f$[a,b)\f$
|
||||||
*/
|
*/
|
||||||
double _random(double a, double b)
|
double _random(double a, double b)
|
||||||
{
|
{
|
||||||
return ((b - a) * (rand() % 100) / 100.f) + a;
|
int r = rand() % 100;
|
||||||
|
return ((b - a) * r / 100.f) + a;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a given n-dimensional data martix to file.
|
* Save a given n-dimensional data martix to file.
|
||||||
*
|
*
|
||||||
* \param[in] fname filename to save in (gets overwriten without confirmation)
|
* \param [in] fname filename to save in (gets overwriten without confirmation)
|
||||||
* \param[in] X matrix to save
|
* \param [in] X matrix to save
|
||||||
* \param[in] num_points rows in the matrix = number of points
|
* \param [in] num_points rows in the matrix = number of points
|
||||||
* \param[in] num_features columns in the matrix = dimensions of points
|
* \param [in] num_features columns in the matrix = dimensions of points
|
||||||
* \returns 0 if all ok
|
* \returns 0 if all ok
|
||||||
* \returns -1 if file creation failed
|
* \returns -1 if file creation failed
|
||||||
*/
|
*/
|
||||||
|
@ -89,7 +90,7 @@ int save_nd_data(const char *fname, double **X, int num_points,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get minimum value and index of the value in a vector
|
* Get minimum value and index of the value in a vector
|
||||||
* \param[in] x vector to search
|
* \param[in] X vector to search
|
||||||
* \param[in] N number of points in the vector
|
* \param[in] N number of points in the vector
|
||||||
* \param[out] val minimum value found
|
* \param[out] val minimum value found
|
||||||
* \param[out] idx index where minimum value was found
|
* \param[out] idx index where minimum value was found
|
||||||
|
@ -111,7 +112,7 @@ void get_min_1d(double const *X, int N, double *val, int *idx)
|
||||||
/**
|
/**
|
||||||
* Update weights of the SOM using Kohonen algorithm
|
* Update weights of the SOM using Kohonen algorithm
|
||||||
*
|
*
|
||||||
* \param[in] X data point
|
* \param[in] x data point
|
||||||
* \param[in,out] W weights matrix
|
* \param[in,out] W weights matrix
|
||||||
* \param[in,out] D temporary vector to store distances
|
* \param[in,out] D temporary vector to store distances
|
||||||
* \param[in] num_out number of output points
|
* \param[in] num_out number of output points
|
||||||
|
@ -164,7 +165,6 @@ void update_weights(double const *x, double *const *W, double *D, int num_out,
|
||||||
*
|
*
|
||||||
* \param[in] X data set
|
* \param[in] X data set
|
||||||
* \param[in,out] W weights matrix
|
* \param[in,out] W weights matrix
|
||||||
* \param[in] D temporary vector to store distances
|
|
||||||
* \param[in] num_samples number of output points
|
* \param[in] num_samples number of output points
|
||||||
* \param[in] num_features number of features per input sample
|
* \param[in] num_features number of features per input sample
|
||||||
* \param[in] num_out number of output points
|
* \param[in] num_out number of output points
|
||||||
|
|
|
@ -6,7 +6,7 @@ const double pi = 3.141592653589793238462643383279502884;
|
||||||
/**
|
/**
|
||||||
give as arguments to the executable two x and y coordinates
|
give as arguments to the executable two x and y coordinates
|
||||||
outputs a polar coordinate
|
outputs a polar coordinate
|
||||||
**/
|
*/
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
double x, y;
|
double x, y;
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* dynamically large number
|
* dynamically large number
|
||||||
**/
|
*/
|
||||||
typedef struct _large_num
|
typedef struct _large_num
|
||||||
{
|
{
|
||||||
char *digits; /**< array to store individual digits */
|
char *digits; /**< array to store individual digits */
|
||||||
|
@ -20,7 +20,7 @@ typedef struct _large_num
|
||||||
/**
|
/**
|
||||||
* create a new large number
|
* create a new large number
|
||||||
* \returns pointer to a large number
|
* \returns pointer to a large number
|
||||||
**/
|
*/
|
||||||
large_num *new_number(void)
|
large_num *new_number(void)
|
||||||
{
|
{
|
||||||
large_num *new_num = (large_num *)malloc(sizeof(large_num));
|
large_num *new_num = (large_num *)malloc(sizeof(large_num));
|
||||||
|
@ -33,7 +33,7 @@ large_num *new_number(void)
|
||||||
/**
|
/**
|
||||||
* delete all memory allocated for large number
|
* delete all memory allocated for large number
|
||||||
* \param[in] num pointer to large_num to delete
|
* \param[in] num pointer to large_num to delete
|
||||||
**/
|
*/
|
||||||
void delete_number(large_num *num)
|
void delete_number(large_num *num)
|
||||||
{
|
{
|
||||||
free(num->digits);
|
free(num->digits);
|
||||||
|
@ -44,7 +44,7 @@ void delete_number(large_num *num)
|
||||||
* add a digit to the large number
|
* add a digit to the large number
|
||||||
* \param[in,out] num
|
* \param[in,out] num
|
||||||
* \param[in] value value of the digit to insert
|
* \param[in] value value of the digit to insert
|
||||||
**/
|
*/
|
||||||
void add_digit(large_num *num, unsigned int value)
|
void add_digit(large_num *num, unsigned int value)
|
||||||
{
|
{
|
||||||
if (value > 9)
|
if (value > 9)
|
||||||
|
@ -62,7 +62,7 @@ void add_digit(large_num *num, unsigned int value)
|
||||||
/**
|
/**
|
||||||
* multiply large number with another integer and
|
* multiply large number with another integer and
|
||||||
* store the result in the same large number
|
* store the result in the same large number
|
||||||
**/
|
*/
|
||||||
void multiply(large_num *num, unsigned long n)
|
void multiply(large_num *num, unsigned long n)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
|
@ -46,7 +46,7 @@
|
||||||
* \param[in] degree degree of polynomial
|
* \param[in] degree degree of polynomial
|
||||||
* \param[in] x point at which to evaluate the polynomial
|
* \param[in] x point at which to evaluate the polynomial
|
||||||
* \returns \f$f(x)\f$
|
* \returns \f$f(x)\f$
|
||||||
**/
|
*/
|
||||||
long double complex poly_function(double *coeffs, unsigned int degree,
|
long double complex poly_function(double *coeffs, unsigned int degree,
|
||||||
long double complex x)
|
long double complex x)
|
||||||
{
|
{
|
||||||
|
@ -91,7 +91,7 @@ char check_termination(long double delta)
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* the comandline inputs are taken as coeffiecients of a polynomial
|
* the comandline inputs are taken as coeffiecients of a polynomial
|
||||||
**/
|
*/
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
double *coeffs = NULL;
|
double *coeffs = NULL;
|
||||||
|
@ -130,7 +130,7 @@ int main(int argc, char **argv)
|
||||||
#if defined(DEBUG) || !defined(NDEBUG)
|
#if defined(DEBUG) || !defined(NDEBUG)
|
||||||
/**
|
/**
|
||||||
* store intermediate values to a CSV file
|
* store intermediate values to a CSV file
|
||||||
**/
|
*/
|
||||||
FILE *log_file = fopen("durand_kerner.log.csv", "wt");
|
FILE *log_file = fopen("durand_kerner.log.csv", "wt");
|
||||||
if (!log_file)
|
if (!log_file)
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* Newton-Raphson interpolation algorithm.
|
* Newton-Raphson interpolation algorithm.
|
||||||
*
|
*
|
||||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||||
**/
|
*/
|
||||||
|
|
||||||
#include <complex.h> /* requires minimum of C99 */
|
#include <complex.h> /* requires minimum of C99 */
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "qr_decompose.h"
|
#include "qr_decompose.h"
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
|
@ -95,7 +96,7 @@ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2,
|
||||||
* \note The matrix \f$A\f$ gets modified
|
* \note The matrix \f$A\f$ gets modified
|
||||||
*
|
*
|
||||||
* \param[in,out] A matrix to compute eigen values for
|
* \param[in,out] A matrix to compute eigen values for
|
||||||
* \param[out] eig_vals resultant vector containing computed eigen values
|
* \param[out] eigen_vals resultant vector containing computed eigen values
|
||||||
* \param[in] mat_size matrix size
|
* \param[in] mat_size matrix size
|
||||||
* \param[in] debug_print 1 to print intermediate Q & R matrices, 0 for not to
|
* \param[in] debug_print 1 to print intermediate Q & R matrices, 0 for not to
|
||||||
* \returns time for computation in seconds
|
* \returns time for computation in seconds
|
||||||
|
|
|
@ -40,7 +40,7 @@ int get_number(FILE *fp, char *buffer, uint8_t *out_int)
|
||||||
/**
|
/**
|
||||||
* Function to add arbitraty length decimal integers stored in an array.
|
* Function to add arbitraty length decimal integers stored in an array.
|
||||||
* a + b = c = new b
|
* a + b = c = new b
|
||||||
**/
|
*/
|
||||||
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
|
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
|
||||||
{
|
{
|
||||||
int carry = 0;
|
int carry = 0;
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
/**
|
/**
|
||||||
* Computes the length of collatz sequence for a given
|
* Computes the length of collatz sequence for a given
|
||||||
* starting number
|
* starting number
|
||||||
**/
|
*/
|
||||||
long long collatz(long long start_num)
|
long long collatz(long long start_num)
|
||||||
{
|
{
|
||||||
long long length = 1;
|
long long length = 1;
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
* and N right options, without preference for order.
|
* and N right options, without preference for order.
|
||||||
* Hence, the path can be be traced in N out of 2N number of ways.
|
* Hence, the path can be be traced in N out of 2N number of ways.
|
||||||
* This is the same as binomial coeeficient.
|
* This is the same as binomial coeeficient.
|
||||||
**/
|
*/
|
||||||
unsigned long long number_of_paths(int N)
|
unsigned long long number_of_paths(int N)
|
||||||
{
|
{
|
||||||
unsigned long long path = 1;
|
unsigned long long path = 1;
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
* Month is identified by an integer -\n
|
* Month is identified by an integer -\n
|
||||||
* > 0 = Jan and 11 = December\n
|
* > 0 = Jan and 11 = December\n
|
||||||
* For February, adjust for leap year outside the function.
|
* For February, adjust for leap year outside the function.
|
||||||
**/
|
*/
|
||||||
char get_month_days(short month)
|
char get_month_days(short month)
|
||||||
{
|
{
|
||||||
if (month == 1) /* February has 28 days. Adjust leap year in the loop */
|
if (month == 1) /* February has 28 days. Adjust leap year in the loop */
|
||||||
|
@ -37,7 +37,7 @@ char get_month_days(short month)
|
||||||
/**
|
/**
|
||||||
* return 1 if input year is a leap year
|
* return 1 if input year is a leap year
|
||||||
* otherwise, return 0
|
* otherwise, return 0
|
||||||
**/
|
*/
|
||||||
char is_leap_year(short year)
|
char is_leap_year(short year)
|
||||||
{
|
{
|
||||||
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
|
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
|
||||||
|
@ -79,10 +79,10 @@ int main(int argc, char **argv)
|
||||||
const short start_year = 1901;
|
const short start_year = 1901;
|
||||||
const short end_year = 2000;
|
const short end_year = 2000;
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6
|
* Let us identify days i.e., Sunday thru Saturday with integers - 0 thru 6
|
||||||
*respectively Jan 1 1901 was a Tuesday
|
* respectively Jan 1 1901 was a Tuesday
|
||||||
**/
|
*/
|
||||||
char start_day = 2;
|
char start_day = 2;
|
||||||
|
|
||||||
for (int year = start_year; year <= end_year; year++)
|
for (int year = start_year; year <= end_year; year++)
|
||||||
|
@ -90,9 +90,10 @@ int main(int argc, char **argv)
|
||||||
char is_leap = is_leap_year(year);
|
char is_leap = is_leap_year(year);
|
||||||
for (char month = 0; month < 12; month++)
|
for (char month = 0; month < 12; month++)
|
||||||
{
|
{
|
||||||
/**
|
/*
|
||||||
* These two for-loops count the start of day for the next month.
|
* These two for-loops count the start of day for the next month.
|
||||||
* Hence, we have to skip the last December count */
|
* Hence, we have to skip the last December count
|
||||||
|
*/
|
||||||
if (year == end_year && month == 11)
|
if (year == end_year && month == 11)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -109,11 +110,11 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Main Algorithm:
|
/* Main Algorithm:
|
||||||
* every week has 7 days hence, the start of next day would be
|
* every week has 7 days hence, the start of next day would be
|
||||||
*modulo 7 add to this, the current start date and ensure the result
|
* modulo 7 add to this, the current start date and ensure the
|
||||||
*is still modulo 7!
|
* result is still modulo 7!
|
||||||
**/
|
*/
|
||||||
start_day = ((days % 7) + start_day) % 7;
|
start_day = ((days % 7) + start_day) % 7;
|
||||||
|
|
||||||
/* If start-day is a Sunday, increment counter */
|
/* If start-day is a Sunday, increment counter */
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
* \brief [Problem 20](https://projecteuler.net/problem=20) solution
|
* \brief [Problem 20](https://projecteuler.net/problem=20) solution
|
||||||
* \author [Krishna Vedala](https://github.com/kvedala)
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||||
*
|
*
|
||||||
* Implementation uses a custom `big_int` structure that can store arbitrarilty
|
* Implementation uses a custom `big_int` structure that can store arbitrarily
|
||||||
* large integer numbers.
|
* large integer numbers.
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -13,12 +13,12 @@
|
||||||
/**
|
/**
|
||||||
* store arbitratily large integer values
|
* store arbitratily large integer values
|
||||||
* as a linked list of digits.
|
* as a linked list of digits.
|
||||||
**/
|
*/
|
||||||
typedef struct _big_int
|
typedef struct _big_int
|
||||||
{
|
{
|
||||||
char value; /** tens place (single digit) */
|
char value; /**< tens place (single digit) */
|
||||||
struct _big_int *next_digit; /** hundreds place */
|
struct _big_int *next_digit; /**< hundreds place */
|
||||||
struct _big_int *prev_digit; /** units place */
|
struct _big_int *prev_digit; /**< units place */
|
||||||
} big_int;
|
} big_int;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
@ -33,7 +33,7 @@ void print_digit(const big_int *my_int)
|
||||||
/**
|
/**
|
||||||
* Function that allocates memory to add another
|
* Function that allocates memory to add another
|
||||||
* digit at the MSB
|
* digit at the MSB
|
||||||
**/
|
*/
|
||||||
big_int *add_digit(big_int *digit, char value)
|
big_int *add_digit(big_int *digit, char value)
|
||||||
{
|
{
|
||||||
if (digit == NULL)
|
if (digit == NULL)
|
||||||
|
@ -72,7 +72,7 @@ big_int *add_digit(big_int *digit, char value)
|
||||||
/**
|
/**
|
||||||
* Function to remove digits preceeding the
|
* Function to remove digits preceeding the
|
||||||
* current digit.
|
* current digit.
|
||||||
**/
|
*/
|
||||||
char remove_digits(big_int *digit, int N)
|
char remove_digits(big_int *digit, int N)
|
||||||
{
|
{
|
||||||
if (digit == NULL)
|
if (digit == NULL)
|
||||||
|
@ -133,10 +133,10 @@ int main(int argc, char **argv)
|
||||||
ptr->value = tmp;
|
ptr->value = tmp;
|
||||||
|
|
||||||
if (i == N)
|
if (i == N)
|
||||||
/**
|
/*
|
||||||
* sum digits on the last iteration
|
* sum digits on the last iteration
|
||||||
* this avoid having another loop over all digits
|
* this avoid having another loop over all digits
|
||||||
**/
|
*/
|
||||||
sum_digits += tmp;
|
sum_digits += tmp;
|
||||||
|
|
||||||
if (ptr->next_digit)
|
if (ptr->next_digit)
|
||||||
|
@ -158,9 +158,9 @@ int main(int argc, char **argv)
|
||||||
printf("%d! = ", N);
|
printf("%d! = ", N);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** Notice that in the loop above, we make sure that at the end of the loop,
|
/* Notice that in the loop above, we make sure that at the end of the loop,
|
||||||
* ptr is pointing to the last digit. Thus we can avoid using another loop.
|
* ptr is pointing to the last digit. Thus we can avoid using another loop.
|
||||||
**/
|
*/
|
||||||
// ptr = &my_int;
|
// ptr = &my_int;
|
||||||
// /* move ptr to the MSB digit */
|
// /* move ptr to the MSB digit */
|
||||||
// while (ptr->next_digit)
|
// while (ptr->next_digit)
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function to return the sum of proper divisors of N
|
* function to return the sum of proper divisors of N
|
||||||
**/
|
*/
|
||||||
unsigned long sum_of_divisors(unsigned int N)
|
unsigned long sum_of_divisors(unsigned int N)
|
||||||
{
|
{
|
||||||
unsigned long sum = 1 + N; /* 1 and itself are always a divisor */
|
unsigned long sum = 1 + N; /* 1 and itself are always a divisor */
|
||||||
|
@ -40,12 +40,12 @@ int main(int argc, char **argv)
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
MAX_N = atoi(argv[1]);
|
MAX_N = atoi(argv[1]);
|
||||||
|
|
||||||
/**<
|
/*
|
||||||
* We use an array of flags to check if a number at the index was:
|
* We use an array of flags to check if a number at the index was:
|
||||||
* not-processed = 0
|
* not-processed = 0
|
||||||
* is amicable = 1
|
* is amicable = 1
|
||||||
* not amicable = -1
|
* not amicable = -1
|
||||||
**/
|
*/
|
||||||
char *flags = (char *)calloc(MAX_N, sizeof(char));
|
char *flags = (char *)calloc(MAX_N, sizeof(char));
|
||||||
|
|
||||||
clock_t start_time = clock();
|
clock_t start_time = clock();
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alphabetical sorting using 'shell sort' algorithm
|
* Alphabetical sorting using 'shell sort' algorithm
|
||||||
**/
|
*/
|
||||||
void shell_sort(char data[][MAX_NAME_LEN], int LEN)
|
void shell_sort(char data[][MAX_NAME_LEN], int LEN)
|
||||||
{
|
{
|
||||||
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||||
|
@ -44,7 +44,7 @@ void shell_sort(char data[][MAX_NAME_LEN], int LEN)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alphabetical sorting using 'lazy sort' algorithm
|
* Alphabetical sorting using 'lazy sort' algorithm
|
||||||
**/
|
*/
|
||||||
void lazy_sort(char data[][MAX_NAME_LEN], int LEN)
|
void lazy_sort(char data[][MAX_NAME_LEN], int LEN)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
|
@ -17,7 +17,7 @@ unsigned long MAX_N = 28123; /**< upper limit of numbers to check */
|
||||||
* -1 if N is deficient
|
* -1 if N is deficient
|
||||||
* 1 if N is abundant
|
* 1 if N is abundant
|
||||||
* 0 if N is perfect
|
* 0 if N is perfect
|
||||||
**/
|
*/
|
||||||
char get_perfect_number(unsigned long N)
|
char get_perfect_number(unsigned long N)
|
||||||
{
|
{
|
||||||
unsigned long sum = 1;
|
unsigned long sum = 1;
|
||||||
|
@ -43,7 +43,7 @@ char get_perfect_number(unsigned long N)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the given number an abundant number (1) or not (0)
|
* Is the given number an abundant number (1) or not (0)
|
||||||
**/
|
*/
|
||||||
unsigned long is_abundant(unsigned long N)
|
unsigned long is_abundant(unsigned long N)
|
||||||
{
|
{
|
||||||
return get_perfect_number(N) == 1 ? 1 : 0;
|
return get_perfect_number(N) == 1 ? 1 : 0;
|
||||||
|
@ -51,7 +51,7 @@ unsigned long is_abundant(unsigned long N)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the next abundant number after N and not including N
|
* Find the next abundant number after N and not including N
|
||||||
**/
|
*/
|
||||||
unsigned long get_next_abundant(unsigned long N)
|
unsigned long get_next_abundant(unsigned long N)
|
||||||
{
|
{
|
||||||
unsigned long i;
|
unsigned long i;
|
||||||
|
@ -65,13 +65,13 @@ unsigned long get_next_abundant(unsigned long N)
|
||||||
* of two abundant numbers.
|
* of two abundant numbers.
|
||||||
* \returns 1 - if yes
|
* \returns 1 - if yes
|
||||||
* \returns 0 - if not
|
* \returns 0 - if not
|
||||||
**/
|
*/
|
||||||
char is_sum_of_abundant(unsigned long N)
|
char is_sum_of_abundant(unsigned long N)
|
||||||
{
|
{
|
||||||
/** optimized logic:
|
/* optimized logic:
|
||||||
* i + j = N where both i and j should be abundant
|
* i + j = N where both i and j should be abundant
|
||||||
* hence we can simply check for j = N - i as we loop through i
|
* hence we can simply check for j = N - i as we loop through i
|
||||||
**/
|
*/
|
||||||
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
|
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
|
||||||
i = get_next_abundant(i))
|
i = get_next_abundant(i))
|
||||||
if (is_abundant(N - i))
|
if (is_abundant(N - i))
|
||||||
|
|
|
@ -22,14 +22,14 @@ long MAX_N = 28123; /**< Limit of numbers to check */
|
||||||
* Using a whole byte to store a binary info would be redundant.
|
* Using a whole byte to store a binary info would be redundant.
|
||||||
* We will use each byte to represent 8 numbers by relying on bits.
|
* We will use each byte to represent 8 numbers by relying on bits.
|
||||||
* This saves memory required by 1/8
|
* This saves memory required by 1/8
|
||||||
**/
|
*/
|
||||||
char *abundant_flags = NULL;
|
char *abundant_flags = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \returns -1 if N is deficient
|
* \returns -1 if N is deficient
|
||||||
* \returns 1 if N is abundant
|
* \returns 1 if N is abundant
|
||||||
* \returns 0 if N is perfect
|
* \returns 0 if N is perfect
|
||||||
**/
|
*/
|
||||||
char get_perfect_number(unsigned long N)
|
char get_perfect_number(unsigned long N)
|
||||||
{
|
{
|
||||||
unsigned long sum = 1;
|
unsigned long sum = 1;
|
||||||
|
@ -55,7 +55,7 @@ char get_perfect_number(unsigned long N)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the given number an abundant number (1) or not (0)
|
* Is the given number an abundant number (1) or not (0)
|
||||||
**/
|
*/
|
||||||
char is_abundant(unsigned long N)
|
char is_abundant(unsigned long N)
|
||||||
{
|
{
|
||||||
// return abundant_flags[N >> 3] & (1 << N % 8) ? 1 : 0;
|
// return abundant_flags[N >> 3] & (1 << N % 8) ? 1 : 0;
|
||||||
|
@ -66,7 +66,7 @@ char is_abundant(unsigned long N)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the next abundant number after N and not including N
|
* Find the next abundant number after N and not including N
|
||||||
**/
|
*/
|
||||||
unsigned long get_next_abundant(unsigned long N)
|
unsigned long get_next_abundant(unsigned long N)
|
||||||
{
|
{
|
||||||
unsigned long i;
|
unsigned long i;
|
||||||
|
@ -81,13 +81,13 @@ unsigned long get_next_abundant(unsigned long N)
|
||||||
* of two abundant numbers.
|
* of two abundant numbers.
|
||||||
* \returns 1 - if yes
|
* \returns 1 - if yes
|
||||||
* \returns 0 - if not
|
* \returns 0 - if not
|
||||||
**/
|
*/
|
||||||
char is_sum_of_abundant(unsigned long N)
|
char is_sum_of_abundant(unsigned long N)
|
||||||
{
|
{
|
||||||
/** optimized logic:
|
/* optimized logic:
|
||||||
* i + j = N where both i and j should be abundant
|
* i + j = N where both i and j should be abundant
|
||||||
* hence we can simply check for j = N - i as we loop through i
|
* hence we can simply check for j = N - i as we loop through i
|
||||||
**/
|
*/
|
||||||
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
|
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
|
||||||
i = get_next_abundant(i))
|
i = get_next_abundant(i))
|
||||||
if (is_abundant(N - i))
|
if (is_abundant(N - i))
|
||||||
|
@ -107,9 +107,9 @@ int main(int argc, char **argv)
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
MAX_N = strtoul(argv[1], NULL, 10);
|
MAX_N = strtoul(argv[1], NULL, 10);
|
||||||
|
|
||||||
/** byte array to store flags to identify abundant numbers
|
/* byte array to store flags to identify abundant numbers
|
||||||
* the flags are identified by bits
|
* the flags are identified by bits
|
||||||
**/
|
*/
|
||||||
abundant_flags = (char *)calloc(MAX_N >> 3, 1);
|
abundant_flags = (char *)calloc(MAX_N >> 3, 1);
|
||||||
if (!abundant_flags)
|
if (!abundant_flags)
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
/**
|
/**
|
||||||
* Function to add arbitraty length decimal integers stored in an array.\n
|
* Function to add arbitraty length decimal integers stored in an array.\n
|
||||||
* a + b = c = new b
|
* a + b = c = new b
|
||||||
**/
|
*/
|
||||||
unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
|
unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
|
||||||
int N)
|
int N)
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,8 +8,6 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0]))
|
|
||||||
|
|
||||||
/** Helper function to print array values */
|
/** Helper function to print array values */
|
||||||
void show_data(int *arr, long len)
|
void show_data(int *arr, long len)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +27,7 @@ inline void swap(int *a, int *b)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optimized algorithm - takes half the time as other
|
* Optimized algorithm - takes half the time as other
|
||||||
**/
|
*/
|
||||||
void shell_sort(int *array, long LEN)
|
void shell_sort(int *array, long LEN)
|
||||||
{
|
{
|
||||||
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
|
||||||
|
|
Loading…
Reference in New Issue