mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
documentation and bug fixes
This commit is contained in:
parent
093b993cc7
commit
ea3f8bbf29
@ -1,15 +1,25 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Prime factorization of positive integers
|
||||
*/
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
// 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<int> prime_numbers;
|
||||
|
||||
/** list of prime factor-pairs */
|
||||
std::vector<std::pair<int, int>> 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";
|
||||
|
@ -1,26 +1,33 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Get list of prime numbers
|
||||
* @see primes_up_to_billion.cpp sieve_of_eratosthenes.cpp
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/** Generate an increasingly large number of primes
|
||||
* and store in a list
|
||||
*/
|
||||
std::vector<int> primes(int max) {
|
||||
max++;
|
||||
std::vector<int> res;
|
||||
std::vector<bool> 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<int> 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;
|
||||
}
|
||||
|
@ -1,8 +1,15 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Compute prime numbers upto 1 billion
|
||||
* @see prime_numbers.cpp sieve_of_eratosthenes.cpp
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
@ -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$
|
||||
* <br/>Space Complexity : \f$O(N)\f$
|
||||
*
|
||||
* @see primes_up_to_billion.cpp prime_numbers.cpp
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user