diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2021-02-22 17:45:14 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2021-02-22 17:45:14 +0800 |
| commit | 572994875f5972916deabeb84f3648ac4640107b (patch) | |
| tree | 1458cfc22c7b80dd025415f73e7c808814bb0db7 /challenge-003/paulo-custodio/cpp/ch-1.cpp | |
| parent | 7021a6ee78be5c0a1bc1a30db583004a88fc7a15 (diff) | |
| parent | 2c26164a5a90aa14a19078d845769d3ec9fbb5ae (diff) | |
| download | perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.tar.gz perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.tar.bz2 perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-003/paulo-custodio/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-003/paulo-custodio/cpp/ch-1.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-003/paulo-custodio/cpp/ch-1.cpp b/challenge-003/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..d9ca7ee9f0 --- /dev/null +++ b/challenge-003/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,52 @@ +/* +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less +or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. + +*/ + +#include <iostream> +#include <deque> +#include <string> +#include <algorithm> + +int min3(int a, int b, int c) { + return std::min(a, std::min(b, c)); +} + +// hamming number generator +std::deque<int> q2, q3, q5; + +int next_hamming() { + // init three queues with 1 + if (q2.empty()) { + q2.push_back(1); + q3.push_back(1); + q5.push_back(1); + } + + // get the smallest of the queue heads + int n = min3(q2.front(), q3.front(), q5.front()); + + // shift used multiples + if (n == q2.front()) q2.pop_front(); + if (n == q3.front()) q3.pop_front(); + if (n == q5.front()) q5.pop_front(); + + // push next multiples + q2.push_back(2*n); + q3.push_back(3*n); + q5.push_back(5*n); + + return n; +} + +int main(int argc, char* argv[]) { + if (argc == 2) { + for (int i = 0; i < atoi(argv[1]); i++) + std::cout << next_hamming() << std::endl; + } +} |
