Created
October 15, 2021 18:33
-
-
Save withs/cb1ce21a7566b69098a18d33f8d46825 to your computer and use it in GitHub Desktop.
palindrome.cc
This file contains 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 "cstdint" | |
#include "chrono" | |
// Mac M1 (Apple clang version 13.0.0) 2.9s | |
// - Highest palindrome -> 98344389 | |
// Ryzen 5 3600x (MinGW-W64 8.1.0) 1.6s | |
// - Highest palindrome -> 98344389 | |
uint16_t intLen(uint64_t forInt) { | |
uint64_t factor = 1; | |
uint16_t intLen; | |
for ( intLen = 0 ; (forInt / factor) != 0 ; intLen++ ) { | |
factor *= 10; | |
} | |
return intLen; | |
} | |
uint64_t intReverse(uint64_t forInt) { | |
uint8_t factor = 1; | |
uint64_t res = 0; | |
for ( uint16_t it = 0 ; forInt != 0 ; it++ ) { | |
res = (res * factor) + (forInt % 10); | |
forInt = forInt / 10; | |
if ( it == 0 ) | |
factor = 10; | |
} | |
return res; | |
} | |
int32_t main(int32_t argc, char const *argv[]) { | |
std::chrono::time_point start = std::chrono::high_resolution_clock::now(); | |
uint64_t highest = 0; | |
uint64_t res = 0; | |
for (uint16_t loop = 1000; loop != 9999 ; ++loop) { | |
for (uint16_t loopy = 1000; loopy != 9999 ; ++loopy) { | |
res = loop*loopy; | |
if ( intReverse(res) == res && res > highest ) | |
highest = res; | |
} | |
} | |
std::chrono::time_point stop = std::chrono::high_resolution_clock::now(); | |
float duration = ((float)(std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count())) / 1000.0f; | |
std::cout << "Highest palindrome -> " << highest << std::endl; | |
std::cout << "Found in -> " << duration << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment