How to resolve the algorithm Zsigmondy numbers step by step in the Java programming language
How to resolve the algorithm Zsigmondy numbers step by step in the Java programming language
Table of Contents
Problem Statement
Zsigmondy numbers n to a, b, are the greatest divisor of an - bn that is coprime to am - bm for all positive integers m < n.
Suppose we set a = 2 and b = 1. (Zs(n,2,1)) For each n, find the divisors of an - bn and return the largest that is coprime to all of am - bm, where m is each of the positive integers 1 to n - 1. When n = 4, 24 - 14 = 15. The divisors of 15 are 1, 3, 5, and 15. For m = 1, 2, 3 we get 2-1, 22-12, 23-13, or 1, 3, 7. The divisors of 15 that are coprime to each are 5 and 1, (1 is always included). The largest coprime divisor is 5, so Zs(4,2,1) = 5.
When n = 6, 26 - 16 = 63; its divisors are 1, 3, 7, 9, 21, 63. The largest divisor coprime to all of 1, 3, 7, 15, 31 is 1, (1 is always included), so Zs(6,2,1) = 1.
If a particular an - bn is prime, then Zs(n,a,b) will be equal to that prime. 25 - 15 = 31 so Zs(5,2,1) = 31.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Zsigmondy numbers step by step in the Java programming language
Here's a detailed explanation of the Java code:
Import Statements:
import java.util.ArrayList;
: Imports theArrayList
class for creating lists.import java.util.Collections;
: Imports theCollections
class for sorting lists.import java.util.List;
: Imports theList
interface for working with lists.
Class ZsigmondyNumbers
:
1. main
Method:
- Purpose: Main entry point of the program.
- Logic:
- Creates a list of
Pair
objects representing different values fora
andb
in Zsigmondy numbers. - For each pair, prints the Zsigmondy numbers for
n
from 1 to 18.
- Creates a list of
2. zsigmondyNumber
Method:
- Purpose: Calculates the Zsigmondy number for given values of
n
,a
, andb
. - Logic:
- Calculates
dn = a^n - b^n
. - If
dn
is prime, returns it. - Finds all divisors of
dn
. - Removes divisors that share a common factor with
a^m - b^m
for anym < n
. - Returns the largest remaining divisor.
- Calculates
3. divisors
Method:
- Purpose: Returns a list of all divisors of a given number.
- Logic:
- Iterates from 1 up to the square root of the number.
- If a divisor is found, adds it and its corresponding pair (if different) to the list.
- Sorts the list and returns it.
4. isPrime
Method:
- Purpose: Determines whether a given number is prime.
- Logic:
- Handles basic cases for numbers less than or equal to 3.
- Uses a 6k ± 1 optimization for primality testing.
5. gcd
Method:
- Purpose: Calculates the greatest common divisor of two numbers using the Euclidean algorithm.
- Logic:
- Repeatedly replaces the larger number with the remainder of their division until one becomes 0.
- Returns the remaining non-zero number as the GCD.
Key Concepts:
- Zsigmondy Numbers: Numbers of the form
a^n - b^n
that have a prime divisor that doesn't dividea^m - b^m
for any smallerm
. - Prime Number: A natural number greater than 1 that has no positive divisors other than 1 and itself.
- Greatest Common Divisor (GCD): The largest positive integer that divides two or more integers without a remainder.
- Modulo Operation (
%
): Used for checking the remainder after division.
Source code in the java programming language
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public final class ZsigmondyNumbers {
public static void main(String[] args) {
record Pair(int a, int b) {}
List<Pair> pairs = List.of( new Pair(2, 1), new Pair(3, 1), new Pair(4, 1), new Pair(5, 1),
new Pair(6, 1), new Pair(7, 1), new Pair(3, 2), new Pair(5, 3), new Pair(7, 3), new Pair(7, 5) );
for ( Pair pair : pairs ) {
System.out.println("Zsigmondy(n, " + pair.a + ", " + pair.b + ")");
for ( int n = 1; n <= 18; n++ ) {
System.out.print(zsigmondyNumber(n, pair.a, pair.b) + " ");
}
System.out.println(System.lineSeparator());
}
}
private static long zsigmondyNumber(int n, int a, int b) {
final long dn = (long) ( Math.pow(a, n) - Math.pow(b, n) );
if ( isPrime(dn) ) {
return dn;
}
List<Long> divisors = divisors(dn);
for ( int m = 1; m < n; m++ ) {
long dm = (long) ( Math.pow(a, m) - Math.pow(b, m) );
divisors.removeIf( d -> gcd(dm, d) > 1 );
}
return divisors.get(divisors.size() - 1);
}
private static List<Long> divisors(long number) {
List<Long> result = new ArrayList<Long>();
for ( long d = 1; d * d <= number; d++ ) {
if ( number % d == 0 ) {
result.add(d);
if ( d * d < number ) {
result.add(number / d);
}
}
}
Collections.sort(result);
return result;
}
private static boolean isPrime(long number) {
if ( number < 2 ) {
return false;
}
if ( number % 2 == 0 ) {
return number == 2;
}
if ( number % 3 == 0 ) {
return number == 3;
}
int delta = 2;
long k = 5;
while ( k * k <= number ) {
if ( number % k == 0 ) {
return false;
}
k += delta;
delta = 6 - delta;
}
return true;
}
private static long gcd(long a, long b) {
while ( b != 0 ) {
long temp = a; a = b; b = temp % b;
}
return a;
}
}
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Tcl programming language
You may also check:How to resolve the algorithm Dot product step by step in the jq programming language
You may also check:How to resolve the algorithm Soundex step by step in the Stata programming language
You may also check:How to resolve the algorithm Population count step by step in the Factor programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Jsish programming language