How to resolve the algorithm Deceptive numbers step by step in the C++ programming language
How to resolve the algorithm Deceptive numbers step by step in the C++ programming language
Table of Contents
Problem Statement
Repunits are numbers that consist entirely of repetitions of the digit one (unity). The notation Rn symbolizes the repunit made up of n ones. Every prime p larger than 5, evenly divides the repunit Rp-1.
The repunit R6 is evenly divisible by 7. 111111 / 7 = 15873 The repunit R42 is evenly divisible by 43. 111111111111111111111111111111111111111111 / 43 = 2583979328165374677002583979328165374677 And so on.
There are composite numbers that also have this same property. They are often referred to as deceptive non-primes or deceptive numbers.
The repunit R90 is evenly divisible by the composite number 91 (=7*13).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Deceptive numbers step by step in the C++ programming language
1. GMP-based Version:
-
Function
is_prime
: Checks if a numbern
is prime using the Miller-Rabin primality test. -
Main Function:
- Initializes a GMP integer
repunit
to 11. - Iterates through odd numbers
n
starting from 3. - Checks if
n
is not divisible by 3 or 5, is not prime, and dividesrepunit
. - Prints out the deceptive number
n
if it meets these conditions. - Increments
repunit
by multiplying it by 100 and adding 11 to represent the repunit multiplication.
- Initializes a GMP integer
2. Direct Implementation Version:
-
Function
power_modulus
: Calculates the modular exponentiation ofbase
raised to the powerexponent
modulomodulus
. -
Function
is_deceptive
:- Checks if a number
n
is deceptive:n
is odd and not divisible by 3 or 5.10^(n-1)
modulon
is equal to 1.n
is not divisible by any divisor between 7 and the square root ofn
with increments of 6.
- Returns
true
if the conditions are met,false
otherwise.
- Checks if a number
-
Main Function:
- Iterates through numbers
n
starting from 7. - Calls
is_deceptive
for eachn
. - Prints out the deceptive number
n
if it meets the conditions. - Increments
n
by 1.
- Iterates through numbers
Source code in the cpp programming language
#include <gmpxx.h>
#include <iomanip>
#include <iostream>
bool is_prime(int n) {
if (n < 2)
return false;
if (n % 2 == 0)
return n == 2;
if (n % 3 == 0)
return n == 3;
for (int p = 5; p * p <= n; p += 4) {
if (n % p == 0)
return false;
p += 2;
if (n % p == 0)
return false;
}
return true;
}
int main() {
std::cout << "First 100 deceptive numbers:\n";
mpz_class repunit = 11;
for (int n = 3, count = 0; count != 100; n += 2) {
if (n % 3 != 0 && n % 5 != 0 && !is_prime(n) &&
mpz_divisible_ui_p(repunit.get_mpz_t(), n))
std::cout << std::setw(6) << n << (++count % 10 == 0 ? '\n' : ' ');
repunit *= 100;
repunit += 11;
}
}
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
uint64_t power_modulus(uint64_t base, uint64_t exponent, const uint64_t& modulus) {
if ( modulus == 1 ) {
return 0;
}
base %= modulus;
uint64_t result = 1;
while ( exponent > 0 ) {
if ( ( exponent & 1 ) == 1 ) {
result = ( result * base ) % modulus;
}
base = ( base * base ) % modulus;
exponent >>= 1;
}
return result;
}
bool is_deceptive(const uint32_t& n) {
if ( n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && power_modulus(10, n - 1, n) == 1 ) {
for ( uint32_t divisor = 7; divisor < sqrt(n); divisor += 6 ) {
if ( n % divisor == 0 || n % ( divisor + 4 ) == 0 ) {
return true;
}
}
}
return false;
}
int main() {
uint32_t n = 7;
uint32_t count = 0;
while ( count < 100 ) {
if ( is_deceptive(n) ) {
std::cout << std::setw(6) << n << ( ++count % 10 == 0 ? "\n" : " " );
}
n += 1;
}
}
You may also check:How to resolve the algorithm Sleep step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm File size step by step in the Ada programming language
You may also check:How to resolve the algorithm Sort stability step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Checkpoint synchronization step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Wren programming language