TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
largest_power.cpp
Go to the documentation of this file.
1
13
14
#include <cassert>
15
#include <cstdint>
16
#include <iostream>
21
namespace
math
{
22
29
uint64_t
largestPower
(uint32_t n,
const
uint16_t& p) {
30
// Initialize result
31
int
x = 0;
32
33
// Calculate result
34
while
(n) {
35
n /= p;
36
x += n;
37
}
38
return
x;
39
}
40
41
}
// namespace math
42
48
static
void
test
() {
49
uint8_t test_case_1 =
math::largestPower
(5, 2);
50
assert(test_case_1 == 3);
51
std::cout <<
"Test 1 Passed!"
<< std::endl;
52
53
uint16_t test_case_2 =
math::largestPower
(10, 3);
54
assert(test_case_2 == 4);
55
std::cout <<
"Test 2 Passed!"
<< std::endl;
56
57
uint32_t test_case_3 =
math::largestPower
(25, 5);
58
assert(test_case_3 == 6);
59
std::cout <<
"Test 3 Passed!"
<< std::endl;
60
61
uint32_t test_case_4 =
math::largestPower
(27, 2);
62
assert(test_case_4 == 23);
63
std::cout <<
"Test 4 Passed!"
<< std::endl;
64
65
uint16_t test_case_5 =
math::largestPower
(7, 3);
66
assert(test_case_5 == 2);
67
std::cout <<
"Test 5 Passed!"
<< std::endl;
68
}
69
74
int
main
() {
75
test
();
// execute the tests
76
return
0;
77
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
math
for assert
math::largestPower
uint64_t largestPower(uint32_t n, const uint16_t &p)
Function to calculate largest power.
Definition
largest_power.cpp:29
math
largest_power.cpp
Generated by
1.18.0