Sequence of non-squares: Difference between revisions
Content deleted Content added
No edit summary |
New post without using external libraries. In addition to an existing post which uses the "Boost" library. |
||
Line 497:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
Up to 1000000, found no square number in the sequence!
</pre>
Alternatively, without using an external library
<syntaxhighlight lang="cpp">
#include <cmath>
#include <cstdint>
#include <iostream>
uint32_t non_square(const uint32_t& n) {
return n + static_cast<uint32_t>(0.5 + sqrt(n));
}
int main() {
std::cout << "The first 22 non-square numbers:" << std::endl;
for ( uint32_t i = 1; i <= 22; ++i ) {
std::cout << non_square(i) << " ";
}
std::cout << std::endl << std::endl;
uint32_t count = 0;
for ( uint32_t i = 1; i < 1'000'000; ++i ) {
double square_root = sqrt(non_square(i));
if ( square_root == floor(square_root) ) {
count++;
}
}
std::cout << "Number of squares less than 1'000'000 produced by the formula: " << count << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
The first 22 non-square numbers:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
Number of squares less than 1'000'000 produced by the formula: 0
</pre>
|