New Carmichaels

This commit is contained in:
pudepiedj 2024-03-08 09:55:28 +00:00
parent 13ee965026
commit 3d938e8803
3 changed files with 121 additions and 0 deletions

View file

@ -6,6 +6,10 @@ set(TARGET Carmichael)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(${TARGET} Carmichael.cpp) add_executable(${TARGET} Carmichael.cpp)
set(TARGET Carmichael2)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_executable(${TARGET} Carmichael2.cpp)
install(TARGETS ${TARGET} RUNTIME) install(TARGETS ${TARGET} RUNTIME)
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
if (WIN32) if (WIN32)

View file

@ -50,6 +50,30 @@ static bool is_prime(size_t n) {
return is_prime; 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<size_t> 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 // Function to check if a number is Carmichael

View file

@ -0,0 +1,93 @@
// Claude 3 CN generator using Korselt's criteria
#include <iostream>
#include <vector>
#include <cmath>
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<size_t> 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<size_t> 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;
}