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 <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// Declaring variables for maintaing prime numbers and to check whether a number
|
/** Declaring variables for maintaing prime numbers and to check whether a
|
||||||
// is prime or not
|
* number is prime or not
|
||||||
|
*/
|
||||||
bool isprime[1000006];
|
bool isprime[1000006];
|
||||||
|
|
||||||
|
/** list of prime numbers */
|
||||||
std::vector<int> prime_numbers;
|
std::vector<int> prime_numbers;
|
||||||
|
|
||||||
|
/** list of prime factor-pairs */
|
||||||
std::vector<std::pair<int, int>> factors;
|
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) {
|
void SieveOfEratosthenes(int N) {
|
||||||
// initializes the array isprime
|
// initializes the array isprime
|
||||||
memset(isprime, true, sizeof 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) {
|
void prime_factorization(int num) {
|
||||||
int number = num;
|
int number = num;
|
||||||
|
|
||||||
@ -46,9 +56,7 @@ void prime_factorization(int num) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/** Main program */
|
||||||
I added a simple UI.
|
|
||||||
*/
|
|
||||||
int main() {
|
int main() {
|
||||||
int num;
|
int num;
|
||||||
std::cout << "\t\tComputes the prime factorization\n\n";
|
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 <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
/** Generate an increasingly large number of primes
|
||||||
|
* and store in a list
|
||||||
|
*/
|
||||||
std::vector<int> primes(int max) {
|
std::vector<int> primes(int max) {
|
||||||
max++;
|
max++;
|
||||||
std::vector<int> res;
|
std::vector<int> res;
|
||||||
std::vector<bool> numbers(max, false);
|
std::vector<bool> numbers(max, false);
|
||||||
for (int i = 2; i < max; i++) {
|
for (int i = 2; i < max; i++) {
|
||||||
if (!numbers[i]) {
|
if (!numbers[i]) {
|
||||||
for (int j = i; j < max; j += i)
|
for (int j = i; j < max; j += i) numbers[j] = true;
|
||||||
numbers[j] = true;
|
|
||||||
res.push_back(i);
|
res.push_back(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** main function */
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Calculate primes up to:\n>> ";
|
std::cout << "Calculate primes up to:\n>> ";
|
||||||
int n;
|
int n;
|
||||||
std::cin >> n;
|
std::cin >> n;
|
||||||
std::vector<int> ans = primes(n);
|
std::vector<int> ans = primes(n);
|
||||||
for (int i = 0; i < ans.size(); i++)
|
for (int i = 0; i < ans.size(); i++) std::cout << ans[i] << ' ';
|
||||||
std::cout << ans[i] << ' ';
|
|
||||||
std::cout << std::endl;
|
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 <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/** array to store the primes */
|
||||||
char prime[100000000];
|
char prime[100000000];
|
||||||
|
|
||||||
|
/** Perform Sieve algorithm */
|
||||||
void Sieve(int64_t n) {
|
void Sieve(int64_t n) {
|
||||||
memset(prime, '1', sizeof(prime)); // intitize '1' to every index
|
memset(prime, '1', sizeof(prime)); // intitize '1' to every index
|
||||||
prime[0] = '0'; // 0 is not prime
|
prime[0] = '0'; // 0 is not prime
|
||||||
@ -15,6 +22,7 @@ void Sieve(int64_t n) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main() {
|
int main() {
|
||||||
Sieve(100000000);
|
Sieve(100000000);
|
||||||
int64_t n;
|
int64_t n;
|
||||||
@ -23,4 +31,6 @@ int main() {
|
|||||||
std::cout << "YES\n";
|
std::cout << "YES\n";
|
||||||
else
|
else
|
||||||
std::cout << "NO\n";
|
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).
|
* that is between 2 to N (as defined in main).
|
||||||
*
|
*
|
||||||
* Time Complexity : O(N * log N)
|
* Time Complexity : \f$O(N \cdot\log N)\f$
|
||||||
* Space Complexity : O(N)
|
* <br/>Space Complexity : \f$O(N)\f$
|
||||||
|
*
|
||||||
|
* @see primes_up_to_billion.cpp prime_numbers.cpp
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
/** Maximum number of primes */
|
||||||
#define MAX 10000000
|
#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.
|
* the multiples.
|
||||||
*/
|
*/
|
||||||
void sieve(int N)
|
void sieve(uint32_t N) {
|
||||||
{
|
isprime[0] = false;
|
||||||
isprime[0] = 0;
|
isprime[1] = false;
|
||||||
isprime[1] = 0;
|
for (uint32_t i = 2; i <= N; i++) {
|
||||||
for (int i = 2; i <= N; i++)
|
if (isprime[i]) {
|
||||||
{
|
for (uint32_t j = (i << 1); j <= N; j += i) {
|
||||||
if (isprime[i])
|
isprime[j] = false;
|
||||||
{
|
|
||||||
for (int j = i * 2; j <= N; j += i)
|
|
||||||
{
|
|
||||||
isprime[j] = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* This function prints out the primes to STDOUT
|
* This function prints out the primes to STDOUT
|
||||||
*/
|
*/
|
||||||
void print(int N)
|
void print(uint32_t N) {
|
||||||
{
|
for (uint32_t i = 1; i <= N; i++) {
|
||||||
for (int i = 1; i <= N; i++)
|
if (isprime[i]) {
|
||||||
{
|
std::cout << i << ' ';
|
||||||
if (isprime[i] == 1)
|
|
||||||
{
|
|
||||||
cout << i << ' ';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cout << '\n';
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* NOTE: This function is important for the
|
* Initialize the array
|
||||||
* initialization of the array.
|
|
||||||
*/
|
*/
|
||||||
void init()
|
void init() {
|
||||||
{
|
for (uint32_t i = 1; i < MAX; i++) {
|
||||||
for (int i = 1; i < MAX; i++)
|
isprime[i] = true;
|
||||||
{
|
|
||||||
isprime[i] = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
/** main function */
|
||||||
{
|
int main() {
|
||||||
int N = 100;
|
uint32_t N = 100;
|
||||||
init();
|
init();
|
||||||
sieve(N);
|
sieve(N);
|
||||||
print(N);
|
print(N);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user