Created
April 26, 2025 12:10
-
-
Save Nusab19/90057234517cb518095288aaaca17d4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <string> | |
#include <iomanip> | |
bool prime(unsigned long long n) { | |
if (n <= 1) return false; | |
if (n <= 3) return true; | |
if (n % 2 == 0 || n % 3 == 0) return false; | |
for (unsigned long long i = 5; i * i <= n; i += 6) { | |
if (n % i == 0 || n % (i + 2) == 0) return false; | |
} | |
return true; | |
} | |
std::string centerString(unsigned long long num, int width) { | |
std::string numStr = std::to_string(num); | |
int padding = width - numStr.length(); | |
int leftPad = padding / 2; | |
int rightPad = padding - leftPad; | |
return std::string(leftPad, ' ') + numStr + std::string(rightPad, ' '); | |
} | |
int main() { | |
unsigned long long a = 10e6; // Can be increased to a much larger value now | |
unsigned long long diff = 0; | |
unsigned long long prev = 2; | |
int gap = 11; | |
for (unsigned long long i = 3; i <= a; i++) { | |
if (prime(i)) { | |
unsigned long long k = i - prev; | |
unsigned long long pd = diff; | |
if (k > diff) { | |
std::cout << centerString(i, gap) << "-" << centerString(prev, gap) << " = " << i - prev << std::endl; | |
} | |
diff = std::max(diff, k); | |
prev = i; | |
} | |
std::cout << i << '\r' << std::flush; | |
double l = log10(static_cast<double>(i)); | |
if (l == static_cast<int>(l)) { | |
std::cout << std::string(33, '-') << std::endl; | |
std::cout << "\nMax diff till 10e" << static_cast<int>(l) << ": " << diff << "\n" << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment