Skip to content

Commit afd4ccb

Browse files
fix: add <cstdint> to math/**
1 parent 2ec9d7f commit afd4ccb

23 files changed

+196
-162
lines changed

math/aliquot_sum.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include <cassert> /// for assert
23+
#include <cstdint> /// for integral typedefs
2324
#include <iostream> /// for IO operations
2425

2526
/**

math/check_factorial.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @author [ewd00010](https://github.com/ewd00010)
1111
*/
1212
#include <cassert> /// for assert
13+
#include <cstdint> /// for integral typedefs
1314
#include <iostream> /// for cout
1415

1516
/**

math/double_factorial.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include <cassert>
13+
#include <cstdint> /// for integral typedefs
1314
#include <iostream>
1415

1516
/** Compute double factorial using iterative method

math/eulers_totient_function.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @file
3-
* @brief Implementation of [Euler's Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
3+
* @brief Implementation of [Euler's
4+
* Totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
45
* @description
56
* Euler Totient Function is also known as phi function.
67
* \f[\phi(n) =
@@ -24,8 +25,9 @@
2425
* @author [Mann Mehta](https://github.com/mann2108)
2526
*/
2627

27-
#include <iostream> /// for IO operations
28-
#include <cassert> /// for assert
28+
#include <cassert> /// for assert
29+
#include <cstdint> /// for integral typedefs
30+
#include <iostream> /// for IO operations
2931

3032
/**
3133
* @brief Mathematical algorithms
@@ -39,12 +41,14 @@ namespace math {
3941
uint64_t phiFunction(uint64_t n) {
4042
uint64_t result = n;
4143
for (uint64_t i = 2; i * i <= n; i++) {
42-
if (n % i != 0) continue;
44+
if (n % i != 0)
45+
continue;
4346
while (n % i == 0) n /= i;
4447

4548
result -= result / i;
4649
}
47-
if (n > 1) result -= result / n;
50+
if (n > 1)
51+
result -= result / n;
4852

4953
return result;
5054
}

math/factorial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313

1414
#include <cassert> /// for assert
15+
#include <cstdint> /// for integral typedefs
1516
#include <iostream> /// for I/O operations
16-
1717
/**
1818
* @namespace
1919
* @brief Mathematical algorithms

math/fibonacci.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* @see fibonacci_large.cpp, fibonacci_fast.cpp, string_fibonacci.cpp
1010
*/
1111
#include <cassert>
12+
#include <cstdint> /// for integral typedefs
1213
#include <iostream>
13-
1414
/**
1515
* Recursively compute sequences
1616
* @param n input
@@ -31,7 +31,7 @@ uint64_t fibonacci(uint64_t n) {
3131
* Function for testing the fibonacci() function with a few
3232
* test cases and assert statement.
3333
* @returns `void`
34-
*/
34+
*/
3535
static void test() {
3636
uint64_t test_case_1 = fibonacci(0);
3737
assert(test_case_1 == 0);
Lines changed: 83 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,116 @@
11
/**
2-
* @file
2+
* @file
33
* @brief This program computes the N^th Fibonacci number in modulo mod
44
* input argument .
55
*
66
* Takes O(logn) time to compute nth Fibonacci number
7-
*
7+
*
88
*
99
* \author [villayatali123](https://github.com/villayatali123)
1010
* \author [unknown author]()
11-
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp, fibonacci_large.cpp
11+
* @see fibonacci.cpp, fibonacci_fast.cpp, string_fibonacci.cpp,
12+
* fibonacci_large.cpp
1213
*/
1314

14-
#include<iostream>
15-
#include<vector>
1615
#include <cassert>
16+
#include <cstdint> /// for integral typedefs
17+
#include <iostream>
18+
#include <vector>
1719

1820
/**
1921
* This function finds nth fibonacci number in a given modulus
2022
* @param n nth fibonacci number
21-
* @param mod modulo number
23+
* @param mod modulo number
2224
*/
23-
uint64_t fibo(uint64_t n , uint64_t mod )
24-
{
25-
std::vector<uint64_t> result(2,0);
26-
std::vector<std::vector<uint64_t>> transition(2,std::vector<uint64_t>(2,0));
27-
std::vector<std::vector<uint64_t>> Identity(2,std::vector<uint64_t>(2,0));
28-
n--;
29-
result[0]=1, result[1]=1;
30-
Identity[0][0]=1; Identity[0][1]=0;
31-
Identity[1][0]=0; Identity[1][1]=1;
32-
33-
transition[0][0]=0;
34-
transition[1][0]=transition[1][1]=transition[0][1]=1;
35-
36-
while(n)
37-
{
38-
if(n%2)
39-
{
40-
std::vector<std::vector<uint64_t>> res(2, std::vector<uint64_t>(2,0));
41-
for(int i=0;i<2;i++)
42-
{
43-
for(int j=0;j<2;j++)
44-
{
45-
for(int k=0;k<2;k++)
46-
{
47-
res[i][j]=(res[i][j]%mod+((Identity[i][k]%mod*transition[k][j]%mod))%mod)%mod;
48-
}
49-
}
50-
}
51-
for(int i=0;i<2;i++)
52-
{
53-
for(int j=0;j<2;j++)
54-
{
55-
Identity[i][j]=res[i][j];
56-
}
57-
}
58-
n--;
59-
}
60-
else{
61-
std::vector<std::vector<uint64_t>> res1(2, std::vector<uint64_t>(2,0));
62-
for(int i=0;i<2;i++)
63-
{
64-
for(int j=0;j<2;j++)
65-
{
66-
for(int k=0;k<2;k++)
67-
{
68-
res1[i][j]=(res1[i][j]%mod+((transition[i][k]%mod*transition[k][j]%mod))%mod)%mod;
69-
}
70-
}
71-
}
72-
for(int i=0;i<2;i++)
73-
{
74-
for(int j=0;j<2;j++)
75-
{
76-
transition[i][j]=res1[i][j];
77-
}
78-
}
79-
n=n/2;
80-
}
81-
}
82-
return ((result[0]%mod*Identity[0][0]%mod)%mod+(result[1]%mod*Identity[1][0]%mod)%mod)%mod;
25+
uint64_t fibo(uint64_t n, uint64_t mod) {
26+
std::vector<uint64_t> result(2, 0);
27+
std::vector<std::vector<uint64_t>> transition(2,
28+
std::vector<uint64_t>(2, 0));
29+
std::vector<std::vector<uint64_t>> Identity(2, std::vector<uint64_t>(2, 0));
30+
n--;
31+
result[0] = 1, result[1] = 1;
32+
Identity[0][0] = 1;
33+
Identity[0][1] = 0;
34+
Identity[1][0] = 0;
35+
Identity[1][1] = 1;
36+
37+
transition[0][0] = 0;
38+
transition[1][0] = transition[1][1] = transition[0][1] = 1;
39+
40+
while (n) {
41+
if (n % 2) {
42+
std::vector<std::vector<uint64_t>> res(2,
43+
std::vector<uint64_t>(2, 0));
44+
for (int i = 0; i < 2; i++) {
45+
for (int j = 0; j < 2; j++) {
46+
for (int k = 0; k < 2; k++) {
47+
res[i][j] =
48+
(res[i][j] % mod +
49+
((Identity[i][k] % mod * transition[k][j] % mod)) %
50+
mod) %
51+
mod;
52+
}
53+
}
54+
}
55+
for (int i = 0; i < 2; i++) {
56+
for (int j = 0; j < 2; j++) {
57+
Identity[i][j] = res[i][j];
58+
}
59+
}
60+
n--;
61+
} else {
62+
std::vector<std::vector<uint64_t>> res1(
63+
2, std::vector<uint64_t>(2, 0));
64+
for (int i = 0; i < 2; i++) {
65+
for (int j = 0; j < 2; j++) {
66+
for (int k = 0; k < 2; k++) {
67+
res1[i][j] =
68+
(res1[i][j] % mod + ((transition[i][k] % mod *
69+
transition[k][j] % mod)) %
70+
mod) %
71+
mod;
72+
}
73+
}
74+
}
75+
for (int i = 0; i < 2; i++) {
76+
for (int j = 0; j < 2; j++) {
77+
transition[i][j] = res1[i][j];
78+
}
79+
}
80+
n = n / 2;
81+
}
82+
}
83+
return ((result[0] % mod * Identity[0][0] % mod) % mod +
84+
(result[1] % mod * Identity[1][0] % mod) % mod) %
85+
mod;
8386
}
8487

8588
/**
8689
* Function to test above algorithm
8790
*/
88-
void test()
89-
{
90-
assert(fibo(6, 1000000007 ) == 8);
91+
void test() {
92+
assert(fibo(6, 1000000007) == 8);
9193
std::cout << "test case:1 passed\n";
92-
assert(fibo(5, 1000000007 ) == 5);
94+
assert(fibo(5, 1000000007) == 5);
9395
std::cout << "test case:2 passed\n";
94-
assert(fibo(10 , 1000000007) == 55);
96+
assert(fibo(10, 1000000007) == 55);
9597
std::cout << "test case:3 passed\n";
96-
assert(fibo(500 , 100) == 25);
98+
assert(fibo(500, 100) == 25);
9799
std::cout << "test case:3 passed\n";
98-
assert(fibo(500 , 10000) == 4125);
100+
assert(fibo(500, 10000) == 4125);
99101
std::cout << "test case:3 passed\n";
100102
std::cout << "--All tests passed--\n";
101103
}
102104

103105
/**
104106
* Main function
105107
*/
106-
int main()
107-
{
108-
test();
109-
uint64_t mod=1000000007;
110-
std::cout<<"Enter the value of N: ";
111-
uint64_t n=0; std::cin>>n;
112-
std::cout<<n<<"th Fibonacci number in modulo " << mod << ": "<< fibo( n , mod) << std::endl;
108+
int main() {
109+
test();
110+
uint64_t mod = 1000000007;
111+
std::cout << "Enter the value of N: ";
112+
uint64_t n = 0;
113+
std::cin >> n;
114+
std::cout << n << "th Fibonacci number in modulo " << mod << ": "
115+
<< fibo(n, mod) << std::endl;
113116
}

math/fibonacci_sum.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
#include <cassert> /// for assert
16+
#include <cstdint> /// for integral typedefs
1617
#include <iostream> /// for std::cin and std::cout
1718
#include <vector> /// for std::vector
1819

math/finding_number_of_digits_in_a_number.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <cassert> /// for assert
2020
#include <cmath> /// for log calculation
21+
#include <cstdint> /// for integral typedefs
2122
#include <iostream> /// for IO operations
2223

2324
/**

math/integral_approximation.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
/**
22
* @file
3-
* @brief Compute integral approximation of the function using [Riemann sum](https://en.wikipedia.org/wiki/Riemann_sum)
4-
* @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.
5-
* One very common application is approximating the area of functions or lines on a graph and the length of curves and other approximations.
6-
* 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.
7-
* 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.
8-
* 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.
9-
* 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.
10-
* \author [Benjamin Walton](https://github.com/bwalton24)
11-
* \author [Shiqi Sheng](https://github.com/shiqisheng00)
3+
* @brief Compute integral approximation of the function using [Riemann
4+
* sum](https://en.wikipedia.org/wiki/Riemann_sum)
5+
* @details In mathematics, a Riemann sum is a certain kind of approximation of
6+
* an integral by a finite sum. It is named after nineteenth-century German
7+
* mathematician Bernhard Riemann. One very common application is approximating
8+
* the area of functions or lines on a graph and the length of curves and other
9+
* approximations. The sum is calculated by partitioning the region into shapes
10+
* (rectangles, trapezoids, parabolas, or cubics) that form a region similar to
11+
* the region being measured, then calculating the area for each of these
12+
* shapes, and finally adding all of these small areas together. This approach
13+
* can be used to find a numerical approximation for a definite integral even if
14+
* the fundamental theorem of calculus does not make it easy to find a
15+
* closed-form solution. Because the region filled by the small shapes is
16+
* usually not the same shape as the region being measured, the Riemann sum will
17+
* differ from the area being measured. This error can be reduced by dividing up
18+
* the region more finely, using smaller and smaller shapes. As the shapes get
19+
* smaller and smaller, the sum approaches the Riemann integral. \author
20+
* [Benjamin Walton](https://github.com/bwalton24) \author [Shiqi
21+
* Sheng](https://github.com/shiqisheng00)
1222
*/
13-
#include <cassert> /// for assert
14-
#include <cmath> /// for mathematical functions
15-
#include <functional> /// for passing in functions
23+
#include <cassert> /// for assert
24+
#include <cmath> /// for mathematical functions
25+
#include <cstdint> /// for integral typedefs
26+
#include <functional> /// for passing in functions
1627
#include <iostream> /// for IO operations
1728

1829
/**

0 commit comments

Comments
 (0)