mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 10:35:34 +08:00
fix: add <cstdint> to math/**
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* @author [ewd00010](https://github.com/ewd00010)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for cout
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream>
|
||||
|
||||
/** Compute double factorial using iterative method
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
|
||||
* @brief Implementation of [Euler's
|
||||
* Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
|
||||
* @description
|
||||
* Euler Totient Function is also known as phi function.
|
||||
* \f[\phi(n) =
|
||||
@@ -24,8 +25,9 @@
|
||||
* @author [Mann Mehta](https://github.com/mann2108)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for IO operations
|
||||
#include <cassert> /// for assert
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
* @brief Mathematical algorithms
|
||||
@@ -39,12 +41,14 @@ namespace math {
|
||||
uint64_t phiFunction(uint64_t n) {
|
||||
uint64_t result = n;
|
||||
for (uint64_t i = 2; i * i <= n; i++) {
|
||||
if (n % i != 0) continue;
|
||||
if (n % i != 0)
|
||||
continue;
|
||||
while (n % i == 0) n /= i;
|
||||
|
||||
result -= result / i;
|
||||
}
|
||||
if (n > 1) result -= result / n;
|
||||
if (n > 1)
|
||||
result -= result / n;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for I/O operations
|
||||
|
||||
/**
|
||||
* @namespace
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
|
||||
*/
|
||||
#include <cassert>
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* Recursively compute sequences
|
||||
* @param n input
|
||||
@@ -31,7 +31,7 @@ uint64_t fibonacci(uint64_t n) {
|
||||
* Function for testing the fibonacci() function with a few
|
||||
* test cases and assert statement.
|
||||
* @returns `void`
|
||||
*/
|
||||
*/
|
||||
static void test() {
|
||||
uint64_t test_case_1 = fibonacci(0);
|
||||
assert(test_case_1 == 0);
|
||||
|
||||
@@ -1,101 +1,103 @@
|
||||
/**
|
||||
* @file
|
||||
* @file
|
||||
* @brief This program computes the N^th Fibonacci number in modulo mod
|
||||
* input argument .
|
||||
*
|
||||
* Takes O(logn) time to compute nth Fibonacci number
|
||||
*
|
||||
*
|
||||
*
|
||||
* \author [villayatali123](https://github.com/villayatali123)
|
||||
* \author [unknown author]()
|
||||
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, fibonacci_large.cpp
|
||||
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp,
|
||||
* fibonacci_large.cpp
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
#include <cassert>
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* This function finds nth fibonacci number in a given modulus
|
||||
* @param n nth fibonacci number
|
||||
* @param mod modulo number
|
||||
* @param mod modulo number
|
||||
*/
|
||||
uint64_t fibo(uint64_t n , uint64_t mod )
|
||||
{
|
||||
std::vector<uint64_t> result(2,0);
|
||||
std::vector<std::vector<uint64_t>> transition(2,std::vector<uint64_t>(2,0));
|
||||
std::vector<std::vector<uint64_t>> Identity(2,std::vector<uint64_t>(2,0));
|
||||
n--;
|
||||
result[0]=1, result[1]=1;
|
||||
Identity[0][0]=1; Identity[0][1]=0;
|
||||
Identity[1][0]=0; Identity[1][1]=1;
|
||||
|
||||
transition[0][0]=0;
|
||||
transition[1][0]=transition[1][1]=transition[0][1]=1;
|
||||
|
||||
while(n)
|
||||
{
|
||||
if(n%2)
|
||||
{
|
||||
std::vector<std::vector<uint64_t>> res(2, std::vector<uint64_t>(2,0));
|
||||
for(int i=0;i<2;i++)
|
||||
{
|
||||
for(int j=0;j<2;j++)
|
||||
{
|
||||
for(int k=0;k<2;k++)
|
||||
{
|
||||
res[i][j]=(res[i][j]%mod+((Identity[i][k]%mod*transition[k][j]%mod))%mod)%mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=0;i<2;i++)
|
||||
{
|
||||
for(int j=0;j<2;j++)
|
||||
{
|
||||
Identity[i][j]=res[i][j];
|
||||
}
|
||||
}
|
||||
n--;
|
||||
}
|
||||
else{
|
||||
std::vector<std::vector<uint64_t>> res1(2, std::vector<uint64_t>(2,0));
|
||||
for(int i=0;i<2;i++)
|
||||
{
|
||||
for(int j=0;j<2;j++)
|
||||
{
|
||||
for(int k=0;k<2;k++)
|
||||
{
|
||||
res1[i][j]=(res1[i][j]%mod+((transition[i][k]%mod*transition[k][j]%mod))%mod)%mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=0;i<2;i++)
|
||||
{
|
||||
for(int j=0;j<2;j++)
|
||||
{
|
||||
transition[i][j]=res1[i][j];
|
||||
}
|
||||
}
|
||||
n=n/2;
|
||||
}
|
||||
}
|
||||
return ((result[0]%mod*Identity[0][0]%mod)%mod+(result[1]%mod*Identity[1][0]%mod)%mod)%mod;
|
||||
uint64_t fibo(uint64_t n, uint64_t mod) {
|
||||
std::vector<uint64_t> result(2, 0);
|
||||
std::vector<std::vector<uint64_t>> transition(2,
|
||||
std::vector<uint64_t>(2, 0));
|
||||
std::vector<std::vector<uint64_t>> Identity(2, std::vector<uint64_t>(2, 0));
|
||||
n--;
|
||||
result[0] = 1, result[1] = 1;
|
||||
Identity[0][0] = 1;
|
||||
Identity[0][1] = 0;
|
||||
Identity[1][0] = 0;
|
||||
Identity[1][1] = 1;
|
||||
|
||||
transition[0][0] = 0;
|
||||
transition[1][0] = transition[1][1] = transition[0][1] = 1;
|
||||
|
||||
while (n) {
|
||||
if (n % 2) {
|
||||
std::vector<std::vector<uint64_t>> res(2,
|
||||
std::vector<uint64_t>(2, 0));
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
for (int k = 0; k < 2; k++) {
|
||||
res[i][j] =
|
||||
(res[i][j] % mod +
|
||||
((Identity[i][k] % mod * transition[k][j] % mod)) %
|
||||
mod) %
|
||||
mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
Identity[i][j] = res[i][j];
|
||||
}
|
||||
}
|
||||
n--;
|
||||
} else {
|
||||
std::vector<std::vector<uint64_t>> res1(
|
||||
2, std::vector<uint64_t>(2, 0));
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
for (int k = 0; k < 2; k++) {
|
||||
res1[i][j] =
|
||||
(res1[i][j] % mod + ((transition[i][k] % mod *
|
||||
transition[k][j] % mod)) %
|
||||
mod) %
|
||||
mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
transition[i][j] = res1[i][j];
|
||||
}
|
||||
}
|
||||
n = n / 2;
|
||||
}
|
||||
}
|
||||
return ((result[0] % mod * Identity[0][0] % mod) % mod +
|
||||
(result[1] % mod * Identity[1][0] % mod) % mod) %
|
||||
mod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to test above algorithm
|
||||
*/
|
||||
void test()
|
||||
{
|
||||
assert(fibo(6, 1000000007 ) == 8);
|
||||
void test() {
|
||||
assert(fibo(6, 1000000007) == 8);
|
||||
std::cout << "test case:1 passed\n";
|
||||
assert(fibo(5, 1000000007 ) == 5);
|
||||
assert(fibo(5, 1000000007) == 5);
|
||||
std::cout << "test case:2 passed\n";
|
||||
assert(fibo(10 , 1000000007) == 55);
|
||||
assert(fibo(10, 1000000007) == 55);
|
||||
std::cout << "test case:3 passed\n";
|
||||
assert(fibo(500 , 100) == 25);
|
||||
assert(fibo(500, 100) == 25);
|
||||
std::cout << "test case:3 passed\n";
|
||||
assert(fibo(500 , 10000) == 4125);
|
||||
assert(fibo(500, 10000) == 4125);
|
||||
std::cout << "test case:3 passed\n";
|
||||
std::cout << "--All tests passed--\n";
|
||||
}
|
||||
@@ -103,11 +105,12 @@ void test()
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
test();
|
||||
uint64_t mod=1000000007;
|
||||
std::cout<<"Enter the value of N: ";
|
||||
uint64_t n=0; std::cin>>n;
|
||||
std::cout<<n<<"th Fibonacci number in modulo " << mod << ": "<< fibo( n , mod) << std::endl;
|
||||
int main() {
|
||||
test();
|
||||
uint64_t mod = 1000000007;
|
||||
std::cout << "Enter the value of N: ";
|
||||
uint64_t n = 0;
|
||||
std::cin >> n;
|
||||
std::cout << n << "th Fibonacci number in modulo " << mod << ": "
|
||||
<< fibo(n, mod) << std::endl;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for log calculation
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,18 +1,29 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Compute integral approximation of the function using [Riemann sum](https://en.wikipedia.org/wiki/Riemann_sum)
|
||||
* @details In mathematics, a Riemann sum is a certain kind of approximation of an integral by a finite sum. It is named after nineteenth-century German mathematician Bernhard Riemann.
|
||||
* One very common application is approximating the area of functions or lines on a graph and the length of curves and other approximations.
|
||||
* The sum is calculated by partitioning the region into shapes (rectangles, trapezoids, parabolas, or cubics) that form a region similar to the region being measured, then calculating the area for each of these shapes, and finally adding all of these small areas together.
|
||||
* This approach can be used to find a numerical approximation for a definite integral even if the fundamental theorem of calculus does not make it easy to find a closed-form solution.
|
||||
* Because the region filled by the small shapes is usually not the same shape as the region being measured, the Riemann sum will differ from the area being measured.
|
||||
* This error can be reduced by dividing up the region more finely, using smaller and smaller shapes. As the shapes get smaller and smaller, the sum approaches the Riemann integral.
|
||||
* \author [Benjamin Walton](https://github.com/bwalton24)
|
||||
* \author [Shiqi Sheng](https://github.com/shiqisheng00)
|
||||
* @brief Compute integral approximation of the function using [Riemann
|
||||
* sum](https://en.wikipedia.org/wiki/Riemann_sum)
|
||||
* @details In mathematics, a Riemann sum is a certain kind of approximation of
|
||||
* an integral by a finite sum. It is named after nineteenth-century German
|
||||
* mathematician Bernhard Riemann. One very common application is approximating
|
||||
* the area of functions or lines on a graph and the length of curves and other
|
||||
* approximations. The sum is calculated by partitioning the region into shapes
|
||||
* (rectangles, trapezoids, parabolas, or cubics) that form a region similar to
|
||||
* the region being measured, then calculating the area for each of these
|
||||
* shapes, and finally adding all of these small areas together. This approach
|
||||
* can be used to find a numerical approximation for a definite integral even if
|
||||
* the fundamental theorem of calculus does not make it easy to find a
|
||||
* closed-form solution. Because the region filled by the small shapes is
|
||||
* usually not the same shape as the region being measured, the Riemann sum will
|
||||
* differ from the area being measured. This error can be reduced by dividing up
|
||||
* the region more finely, using smaller and smaller shapes. As the shapes get
|
||||
* smaller and smaller, the sum approaches the Riemann integral. \author
|
||||
* [Benjamin Walton](https://github.com/bwalton24) \author [Shiqi
|
||||
* Sheng](https://github.com/shiqisheng00)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for mathematical functions
|
||||
#include <functional> /// for passing in functions
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for mathematical functions
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <functional> /// for passing in functions
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cmath> /// for `std::sqrt`
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
#include <limits> /// for numeric_limits
|
||||
|
||||
/**
|
||||
* @brief This is the function that calculates the fast inverse square root.
|
||||
* The following code is the fast inverse square root implementation from
|
||||
|
||||
@@ -1,41 +1,42 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Algorithm to find largest x such that p^x divides n! (factorial) using Legendre's Formula.
|
||||
* @details Given an integer n and a prime number p, the task is to find the largest x such that
|
||||
* p^x (p raised to power x) divides n! (factorial). This will be done using Legendre's formula:
|
||||
* x = [n/(p^1)] + [n/(p^2)] + [n/(p^3)] + \ldots + 1
|
||||
* @see more on https://math.stackexchange.com/questions/141196/highest-power-of-a-prime-p-dividing-n
|
||||
* @brief Algorithm to find largest x such that p^x divides n! (factorial) using
|
||||
* Legendre's Formula.
|
||||
* @details Given an integer n and a prime number p, the task is to find the
|
||||
* largest x such that p^x (p raised to power x) divides n! (factorial). This
|
||||
* will be done using Legendre's formula: x = [n/(p^1)] + [n/(p^2)] + [n/(p^3)]
|
||||
* + \ldots + 1
|
||||
* @see more on
|
||||
* https://math.stackexchange.com/questions/141196/highest-power-of-a-prime-p-dividing-n
|
||||
* @author [uday6670](https://github.com/uday6670)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <cassert> /// for assert
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
*/
|
||||
namespace math {
|
||||
|
||||
/**
|
||||
* @brief Function to calculate largest power
|
||||
* @param n number
|
||||
* @param p prime number
|
||||
* @returns largest power
|
||||
*/
|
||||
uint64_t largestPower(uint32_t n, const uint16_t& p)
|
||||
{
|
||||
// Initialize result
|
||||
int x = 0;
|
||||
|
||||
// Calculate result
|
||||
while (n)
|
||||
{
|
||||
n /= p;
|
||||
x += n;
|
||||
}
|
||||
return x;
|
||||
/**
|
||||
* @brief Function to calculate largest power
|
||||
* @param n number
|
||||
* @param p prime number
|
||||
* @returns largest power
|
||||
*/
|
||||
uint64_t largestPower(uint32_t n, const uint16_t& p) {
|
||||
// Initialize result
|
||||
int x = 0;
|
||||
|
||||
// Calculate result
|
||||
while (n) {
|
||||
n /= p;
|
||||
x += n;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
} // namespace math
|
||||
|
||||
@@ -43,36 +44,34 @@ namespace math {
|
||||
* @brief Function for testing largestPower function.
|
||||
* test cases and assert statement.
|
||||
* @returns `void`
|
||||
*/
|
||||
static void test()
|
||||
{
|
||||
uint8_t test_case_1 = math::largestPower(5,2);
|
||||
assert(test_case_1==3);
|
||||
std::cout<<"Test 1 Passed!"<<std::endl;
|
||||
|
||||
uint16_t test_case_2 = math::largestPower(10,3);
|
||||
assert(test_case_2==4);
|
||||
std::cout<<"Test 2 Passed!"<<std::endl;
|
||||
|
||||
uint32_t test_case_3 = math::largestPower(25,5);
|
||||
assert(test_case_3==6);
|
||||
std::cout<<"Test 3 Passed!"<<std::endl;
|
||||
|
||||
uint32_t test_case_4 = math::largestPower(27,2);
|
||||
assert(test_case_4==23);
|
||||
std::cout<<"Test 4 Passed!"<<std::endl;
|
||||
|
||||
uint16_t test_case_5 = math::largestPower(7,3);
|
||||
assert(test_case_5==2);
|
||||
std::cout<<"Test 5 Passed!"<<std::endl;
|
||||
}
|
||||
*/
|
||||
static void test() {
|
||||
uint8_t test_case_1 = math::largestPower(5, 2);
|
||||
assert(test_case_1 == 3);
|
||||
std::cout << "Test 1 Passed!" << std::endl;
|
||||
|
||||
uint16_t test_case_2 = math::largestPower(10, 3);
|
||||
assert(test_case_2 == 4);
|
||||
std::cout << "Test 2 Passed!" << std::endl;
|
||||
|
||||
uint32_t test_case_3 = math::largestPower(25, 5);
|
||||
assert(test_case_3 == 6);
|
||||
std::cout << "Test 3 Passed!" << std::endl;
|
||||
|
||||
uint32_t test_case_4 = math::largestPower(27, 2);
|
||||
assert(test_case_4 == 23);
|
||||
std::cout << "Test 4 Passed!" << std::endl;
|
||||
|
||||
uint16_t test_case_5 = math::largestPower(7, 3);
|
||||
assert(test_case_5 == 2);
|
||||
std::cout << "Test 5 Passed!" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
test(); // execute the tests
|
||||
int main() {
|
||||
test(); // execute the tests
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
* @author [Ashish Daulatabad](https://github.com/AshishYUO)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector STL
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
* @author [Neha Hasija](https://github.com/neha-hasija17)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,8 +17,8 @@
|
||||
* @author [Shri2206](https://github.com/Shri2206)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for IO operations
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for std::cout
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
*/
|
||||
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace math
|
||||
* @brief Mathematical algorithms
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@@ -21,7 +22,8 @@
|
||||
* a prime p starting from p * p since all of the lower multiples
|
||||
* have been already eliminated.
|
||||
* @param N number of primes to check
|
||||
* @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
|
||||
* @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is
|
||||
* a prime or not
|
||||
*/
|
||||
std::vector<bool> sieve(uint32_t N) {
|
||||
std::vector<bool> is_prime(N + 1, true);
|
||||
@@ -39,7 +41,8 @@ std::vector<bool> sieve(uint32_t N) {
|
||||
/**
|
||||
* This function prints out the primes to STDOUT
|
||||
* @param N number of primes to check
|
||||
* @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
|
||||
* @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is
|
||||
* a prime or not
|
||||
*/
|
||||
void print(uint32_t N, const std::vector<bool> &is_prime) {
|
||||
for (uint32_t i = 2; i <= N; i++) {
|
||||
@@ -54,9 +57,11 @@ void print(uint32_t N, const std::vector<bool> &is_prime) {
|
||||
* Test implementations
|
||||
*/
|
||||
void tests() {
|
||||
// 0 1 2 3 4 5 6 7 8 9 10
|
||||
std::vector<bool> ans{false, false, true, true, false, true, false, true, false, false, false};
|
||||
assert(sieve(10) == ans);
|
||||
// 0 1 2 3 4 5 6 7 8
|
||||
// 9 10
|
||||
std::vector<bool> ans{false, false, true, true, false, true,
|
||||
false, true, false, false, false};
|
||||
assert(sieve(10) == ans);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* @see fibonacci_large.cpp, fibonacci_fast.cpp, fibonacci.cpp
|
||||
*/
|
||||
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream>
|
||||
#ifdef _MSC_VER
|
||||
#include <string> // use this for MS Visual C
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* @author [muskan0719](https://github.com/muskan0719)
|
||||
*/
|
||||
#include <cassert> /// for assert
|
||||
#include <cstdint> /// for integral typedefs
|
||||
#include <iostream> /// for std::cin and std::cout
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user