From 3d938e880307869483aaa8fd1fffbc2c48cb690c Mon Sep 17 00:00:00 2001 From: pudepiedj Date: Fri, 8 Mar 2024 09:55:28 +0000 Subject: [PATCH] New Carmichaels --- examples/cmap-example/CMakeLists.txt | 4 ++ examples/cmap-example/Carmichael.cpp | 24 +++++++ examples/cmap-example/Carmichael2.cpp | 93 +++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 examples/cmap-example/Carmichael2.cpp diff --git a/examples/cmap-example/CMakeLists.txt b/examples/cmap-example/CMakeLists.txt index 1fbfcf603..fa06a49d9 100644 --- a/examples/cmap-example/CMakeLists.txt +++ b/examples/cmap-example/CMakeLists.txt @@ -6,6 +6,10 @@ set(TARGET Carmichael) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) add_executable(${TARGET} Carmichael.cpp) +set(TARGET Carmichael2) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +add_executable(${TARGET} Carmichael2.cpp) + install(TARGETS ${TARGET} RUNTIME) target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT}) if (WIN32) diff --git a/examples/cmap-example/Carmichael.cpp b/examples/cmap-example/Carmichael.cpp index eb2f60260..c0e3eae07 100644 --- a/examples/cmap-example/Carmichael.cpp +++ b/examples/cmap-example/Carmichael.cpp @@ -50,6 +50,30 @@ static bool is_prime(size_t n) { return is_prime; } +static bool is_carmichael_korselt(size_t n) { + if (n <= 1) return false; + if (n % 2 == 0 && n != 2) return false; // Even numbers except 2 can't be Carmichael + + // Check for square-free property + size_t sqrt_n = sqrt(n); + for (size_t i = 3; i <= sqrt_n; i += 2) { + if (n % (i * i) == 0) return false; // Perfect square factor found + } + + // Check Korselt's condition for each prime factor + vector factors; + for (size_t p = 3; p * p <= n; p += 2) { // Check only odd primes + if (is_prime(p) && n % p == 0) { + factors.push_back(p); + if ((p - 1) % (n - 1) != 0) return false; // Doesn't satisfy Korselt's criterion + } + } + + // All prime factors satisfy Korselt's condition - n might be Carmichael + // You can optionally do additional checks here or return true; + + return true; // Replace with further checks or return based on your needs +} // Function to check if a number is Carmichael diff --git a/examples/cmap-example/Carmichael2.cpp b/examples/cmap-example/Carmichael2.cpp new file mode 100644 index 000000000..dae8301eb --- /dev/null +++ b/examples/cmap-example/Carmichael2.cpp @@ -0,0 +1,93 @@ +// Claude 3 CN generator using Korselt's criteria + +#include +#include +#include + +using namespace std; + +// Function to check if a number is prime +static bool isPrime(size_t n) { + if (n <= 1) + return false; + if (n <= 3) + return true; + if (n % 2 == 0 || n % 3 == 0) + return false; + + for (size_t i = 5; i * i <= n; i += 6) + if (n % i == 0 || n % (i + 2) == 0) + return false; + + return true; +} + +// Function to calculate the value of (a^n) % n +static size_t modularExponentiation(size_t a, size_t n, size_t modulus) { + size_t result = 1; + a %= modulus; + + while (n > 0) { + if (n & 1) + result = (result * a) % modulus; + a = (a * a) % modulus; + n >>= 1; + } + + return result; +} + +// Function to check if a number is a Carmichael number +static bool isCarmichael(size_t n) { + if (isPrime(n)) + return false; + + vector divisors; + for (size_t i = 2; i * i <= n; i++) { + if (n % i == 0) { + divisors.push_back(i); + if (i != n / i) + divisors.push_back(n / i); + } + } + + for (size_t a = 2; a < n; a++) { + if (isPrime(a)) { + bool isCarmichael = true; + for (size_t d : divisors) { + if (modularExponentiation(a, d, n) != 1) { + isCarmichael = false; + break; + } + } + if (isCarmichael) + return true; + } + } + + return false; +} + +int main() { + size_t upperLimit; + cout << "Enter the upper limit: "; + cin >> upperLimit; + + vector carmichaelNumbers; + + for (size_t i = 2; i <= upperLimit; i++) { + if (isCarmichael(i)) + carmichaelNumbers.push_back(i); + } + + if (carmichaelNumbers.empty()) + cout << "No Carmichael numbers found up to " << upperLimit << endl; + else { + cout << "Carmichael numbers up to " << upperLimit << ":" << endl; + for (size_t n : carmichaelNumbers) + cout << n << " "; + cout << endl; + } + + return 0; +}