How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Java programming language
How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Java programming language
Table of Contents
Problem Statement
Find the composite numbers k in base 10, that have no single digit prime factors and whose prime factors are all a substring of k.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Java programming language
The Java program CompositeNumbersK
finds the first 20 composite numbers k
which satisfy the following conditions:
k
is divisible by 11^2, but not by 3, 5, or 7.- The prime factors of
k
are all digits ofk
.
The program uses the Pollard's rho algorithm to find the prime factors of k
. The algorithm starts with a random number x
and iterates the following formula:
x = (x^2 + c) mod n
where c
is a constant and n
is the number to be factored. The algorithm stops when x
and 2x
have a common factor. This factor is then used to find the prime factors of n
.
Here's a breakdown of the Java program:
-
The
main
method initializes a listresult
to store the first 20 composite numbers that satisfy the given conditions. It sets the initial value ofk
to 11^2 and initializes a loop that continues until 20 numbers have been added to the list. -
Inside the loop, the program checks if
k
is divisible by 3, 5, or 7. If it is,k
is incremented by 2 and the loop continues. -
If
k
is not divisible by 3, 5, or 7, the program calls theprimeFactors
method to find the prime factors ofk
. Ifk
has more than one prime factor and all of its prime factors are digits ofk
,k
is added to theresult
list. -
After finding the composite numbers that satisfy the given conditions, the program prints them to the console.
-
The
primeFactors
method takes an integeraK
as input and returns a list of its prime factors. The method first checks ifaK
is less than or equal to 1. If it is, the method returns an empty list. -
If
aK
is greater than 1, the method convertsaK
to aBigInteger
and checks if it is a probable prime number. If it is, the method addsaK
to the list of prime factors and returns the list. -
If
aK
is not a probable prime number, the method calls thepollardsRho
method to find a factor ofaK
. The method then recursively calls itself to find the prime factors of the factor andaK
divided by the factor. The prime factors are then added to the list and returned. -
The
pollardsRho
method takes aBigInteger
aN
as input and returns a factor ofaN
. The method first initializes a constantconstant
and a randomBigInteger
x
. The method then enters a loop that continues until the greatest common divisor (GCD) ofx
and2x
is greater than 1. -
Inside the loop,
x
is updated using the Pollard's rho algorithm formula. The GCD ofx
and2x
is then calculated and checked. If the GCD is greater than 1, the method returns the GCD.
Source code in the java programming language
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public final class CompositeNumbersK {
public static void main(String[] aArgs) {
int k = 11 * 11;
List<Integer> result = new ArrayList<Integer>();
while ( result.size() < 20 ) {
while ( k % 3 == 0 || k % 5 == 0 || k % 7 == 0 ) {
k += 2;
}
List<Integer> factors = primeFactors(k);
if ( factors.size() > 1 ) {
String stringK = String.valueOf(k);
if ( factors.stream().allMatch( factor -> stringK.indexOf(String.valueOf(factor)) >= 0 ) ) {
result.add(k);
}
}
k += 2;
}
for ( int i = 0; i < result.size(); i++ ) {
System.out.print(String.format("%10d%s", result.get(i), ( i == 9 || i == 19 ? "\n" : "" )));
}
}
private static List<Integer> primeFactors(int aK) {
List<Integer> result = new ArrayList<Integer>();
if ( aK <= 1 ) {
return result;
}
BigInteger bigK = BigInteger.valueOf(aK);
if ( bigK.isProbablePrime(CERTAINTY_LEVEL) ) {
result.add(aK);
return result;
}
final int divisor = pollardsRho(bigK).intValueExact();
result.addAll(primeFactors(divisor));
result.addAll(primeFactors(aK / divisor));
Collections.sort(result);
return result;
}
private static BigInteger pollardsRho(BigInteger aN) {
final BigInteger constant = new BigInteger(aN.bitLength(), RANDOM);
BigInteger x = new BigInteger(aN.bitLength(), RANDOM);
BigInteger xx = x;
BigInteger divisor = null;
if ( aN.mod(BigInteger.TWO).signum() == 0 ) {
return BigInteger.TWO;
}
do {
x = x.multiply(x).mod(aN).add(constant).mod(aN);
xx = xx.multiply(xx).mod(aN).add(constant).mod(aN);
xx = xx.multiply(xx).mod(aN).add(constant).mod(aN);
divisor = x.subtract(xx).gcd(aN);
} while ( divisor.compareTo(BigInteger.ONE) == 0 );
return divisor;
}
private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
private static final int CERTAINTY_LEVEL = 10;
}
You may also check:How to resolve the algorithm AVL tree step by step in the Phix programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Intercal programming language
You may also check:How to resolve the algorithm Problem of Apollonius step by step in the VBA programming language
You may also check:How to resolve the algorithm Program termination step by step in the BQN programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Objective-C programming language