From ea3f8bbf29381b0a999891d6590c934cd2cdbacc Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 16:45:33 -0400 Subject: [PATCH] documentation and bug fixes --- math/prime_factorization.cpp | 22 ++++++---- math/prime_numbers.cpp | 15 +++++-- math/primes_up_to_billion.cpp | 10 +++++ math/sieve_of_eratosthenes.cpp | 74 ++++++++++++++++------------------ 4 files changed, 71 insertions(+), 50 deletions(-) diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index c018de666..d0e05cb4c 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -1,15 +1,25 @@ +/** + * @file + * @brief Prime factorization of positive integers + */ #include #include #include #include -// Declaring variables for maintaing prime numbers and to check whether a number -// is prime or not +/** Declaring variables for maintaing prime numbers and to check whether a + * number is prime or not + */ bool isprime[1000006]; + +/** list of prime numbers */ std::vector prime_numbers; + +/** list of prime factor-pairs */ std::vector> factors; -// Calculating prime number upto a given range +/** Calculating prime number upto a given range + */ void SieveOfEratosthenes(int N) { // initializes the array isprime memset(isprime, true, sizeof isprime); @@ -25,7 +35,7 @@ void SieveOfEratosthenes(int N) { } } -// Prime factorization of a number +/** Prime factorization of a number */ void prime_factorization(int num) { int number = num; @@ -46,9 +56,7 @@ void prime_factorization(int num) { } } -/* - I added a simple UI. -*/ +/** Main program */ int main() { int num; std::cout << "\t\tComputes the prime factorization\n\n"; diff --git a/math/prime_numbers.cpp b/math/prime_numbers.cpp index 7264ff528..4dd54f136 100644 --- a/math/prime_numbers.cpp +++ b/math/prime_numbers.cpp @@ -1,26 +1,33 @@ +/** + * @file + * @brief Get list of prime numbers + * @see primes_up_to_billion.cpp sieve_of_eratosthenes.cpp + */ #include #include +/** Generate an increasingly large number of primes + * and store in a list + */ std::vector primes(int max) { max++; std::vector res; std::vector numbers(max, false); for (int i = 2; i < max; i++) { if (!numbers[i]) { - for (int j = i; j < max; j += i) - numbers[j] = true; + for (int j = i; j < max; j += i) numbers[j] = true; res.push_back(i); } } return res; } +/** main function */ int main() { std::cout << "Calculate primes up to:\n>> "; int n; std::cin >> n; std::vector ans = primes(n); - for (int i = 0; i < ans.size(); i++) - std::cout << ans[i] << ' '; + for (int i = 0; i < ans.size(); i++) std::cout << ans[i] << ' '; std::cout << std::endl; } diff --git a/math/primes_up_to_billion.cpp b/math/primes_up_to_billion.cpp index d8f5a9f9d..4fb79a15e 100644 --- a/math/primes_up_to_billion.cpp +++ b/math/primes_up_to_billion.cpp @@ -1,8 +1,15 @@ +/** + * @file + * @brief Compute prime numbers upto 1 billion + * @see prime_numbers.cpp sieve_of_eratosthenes.cpp + */ #include #include +/** array to store the primes */ char prime[100000000]; +/** Perform Sieve algorithm */ void Sieve(int64_t n) { memset(prime, '1', sizeof(prime)); // intitize '1' to every index prime[0] = '0'; // 0 is not prime @@ -15,6 +22,7 @@ void Sieve(int64_t n) { } } +/** Main function */ int main() { Sieve(100000000); int64_t n; @@ -23,4 +31,6 @@ int main() { std::cout << "YES\n"; else std::cout << "NO\n"; + + return 0; } diff --git a/math/sieve_of_eratosthenes.cpp b/math/sieve_of_eratosthenes.cpp index e600e480d..d8fa70531 100644 --- a/math/sieve_of_eratosthenes.cpp +++ b/math/sieve_of_eratosthenes.cpp @@ -1,69 +1,65 @@ -/* - * Sieve of Eratosthenes is an algorithm to find the primes +/** + * @file + * @brief Get list of prime numbers using Sieve of Eratosthenes + * Sieve of Eratosthenes is an algorithm to find the primes * that is between 2 to N (as defined in main). * - * Time Complexity : O(N * log N) - * Space Complexity : O(N) + * Time Complexity : \f$O(N \cdot\log N)\f$ + *
Space Complexity : \f$O(N)\f$ + * + * @see primes_up_to_billion.cpp prime_numbers.cpp */ #include -using namespace std; +/** Maximum number of primes */ #define MAX 10000000 -int isprime[MAX]; +/** array to store the primes */ +bool isprime[MAX]; -/* - * This is the function that finds the primes and eliminates +/** + * This is the function that finds the primes and eliminates * the multiples. */ -void sieve(int N) -{ - isprime[0] = 0; - isprime[1] = 0; - for (int i = 2; i <= N; i++) - { - if (isprime[i]) - { - for (int j = i * 2; j <= N; j += i) - { - isprime[j] = 0; +void sieve(uint32_t N) { + isprime[0] = false; + isprime[1] = false; + for (uint32_t i = 2; i <= N; i++) { + if (isprime[i]) { + for (uint32_t j = (i << 1); j <= N; j += i) { + isprime[j] = false; } } } } -/* +/** * This function prints out the primes to STDOUT */ -void print(int N) -{ - for (int i = 1; i <= N; i++) - { - if (isprime[i] == 1) - { - cout << i << ' '; +void print(uint32_t N) { + for (uint32_t i = 1; i <= N; i++) { + if (isprime[i]) { + std::cout << i << ' '; } } - cout << '\n'; + std::cout << std::endl; } -/* - * NOTE: This function is important for the - * initialization of the array. +/** + * Initialize the array */ -void init() -{ - for (int i = 1; i < MAX; i++) - { - isprime[i] = 1; +void init() { + for (uint32_t i = 1; i < MAX; i++) { + isprime[i] = true; } } -int main() -{ - int N = 100; +/** main function */ +int main() { + uint32_t N = 100; init(); sieve(N); print(N); + return 0; }