How to resolve the algorithm Jacobsthal numbers step by step in the Java programming language
How to resolve the algorithm Jacobsthal numbers step by step in the Java programming language
Table of Contents
Problem Statement
Jacobsthal numbers are an integer sequence related to Fibonacci numbers. Similar to Fibonacci, where each term is the sum of the previous two terms, each term is the sum of the previous, plus twice the one before that. Traditionally the sequence starts with the given terms 0, 1. Terms may be calculated directly using one of several possible formulas:
Jacobsthal-Lucas numbers are very similar. They have the same recurrence relationship, the only difference is an initial starting value J0 = 2 rather than J0 = 0. Terms may be calculated directly using one of several possible formulas: Jacobsthal oblong numbers is the sequence obtained from multiplying each Jacobsthal number Jn by its direct successor Jn+1.
Jacobsthal primes are Jacobsthal numbers that are prime.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Jacobsthal numbers step by step in the Java programming language
Jacobsthal Numbers
Jacobsthal numbers are a sequence of integers that are defined recursively as follows:
- J(0) = 0
- J(1) = 1
- J(n) = J(n-1) + 2J(n-2) for n >= 2.
Jacobsthal-Lucas Numbers Jacobsthal-Lucas numbers are a related sequence of integers that are defined recursively as follows:
- L(0) = 2
- L(1) = 1
- L(n) = L(n-1) + L(n-2) for n >= 2.
Jacobsthal Oblong Numbers Jacobsthal oblong numbers are a sequence of integers that are defined recursively as follows:
- J_o(0) = 0
- J_o(1) = 1
- J_o(n) = J(n-1)J(n) for n >= 2.
Jacobsthal Prime Numbers Jacobsthal prime numbers are prime numbers that are also Jacobsthal numbers.
Implementation
The provided Java code defines a class called JacobsthalNumbers
that contains methods to calculate and print the Jacobsthal numbers, Jacobsthal-Lucas numbers, Jacobsthal oblong numbers, and Jacobsthal prime numbers.
- The
main
method prints the first 30 Jacobsthal numbers, the first 30 Jacobsthal-Lucas numbers, the first 20 Jacobsthal oblong numbers, and the first 10 Jacobsthal prime numbers. - The
jacobsthalNumber
method calculates the nth Jacobsthal number using the recursive formula. - The
jacobsthalLucasNumber
method calculates the nth Jacobsthal-Lucas number using the recursive formula. - The
jacobsthalOblongNumber
method calculates the nth Jacobsthal oblong number using the recursive formula. - The
jacobsthalPrimeNumber
method calculates the nth Jacobsthal prime number by repeatedly generating Jacobsthal numbers until a probable prime is found. - The
parityValue
method returns the parity of the given index.
Example Output The following is the output of the code:
The first 30 Jacobsthal Numbers:
0 1 1 3 5 11 21 43 85 171
341 683 1365 2731 5461 10923 21845 43691 87381 174763
349525 699051 1398103 2796207 5592415 11184831 22369663 44739327 89478655 178957311
357914623 715829247 1431658495 2863316991 5726633983 11453267967 22906535935 45813071871 91626143743 183252287487
The first 30 Jacobsthal-Lucas Numbers:
2 1 3 4 7 11 18 29 47 76
123 199 322 521 843 1364 2207 3571 5778 9349
15127 24476 39603 64079 103682 167761 271443 439204 710647 1150981
1861628 3012609 4874237 7886846 12761083 20647929 33409012 54056941 87465953 141522894
228988847 370511741 599500588 970012329 1569512917 2540025246 4109538163 6650129273 10759667436 17409796709
The first 20 Jacobsthal Oblong Numbers:
0 0 1 2 5 14
44 149 538 2030 8067 33122
141484 612634 2724310 12271436 57089031 270259006
1304609064 6329261184 31099162498 155640386686 787033125633
4049729132508 21014905495369 109453885820858 572620827223884
The first 10 Jacobsthal Primes:
2
3
7
19
37
109
277
673
1627
4051
Source code in the java programming language
import java.math.BigInteger;
public final class JacobsthalNumbers {
public static void main(String[] aArgs) {
System.out.println("The first 30 Jacobsthal Numbers:");
for ( int i = 0; i < 6; i++ ) {
for ( int k = 0; k < 5; k++ ) {
System.out.print(String.format("%15s", jacobsthalNumber(i * 5 + k)));
}
System.out.println();
}
System.out.println();
System.out.println("The first 30 Jacobsthal-Lucas Numbers:");
for ( int i = 0; i < 6; i++ ) {
for ( int k = 0; k < 5; k++ ) {
System.out.print(String.format("%15s", jacobsthalLucasNumber(i * 5 + k)));
}
System.out.println();
}
System.out.println();
System.out.println("The first 20 Jacobsthal oblong Numbers:");
for ( int i = 0; i < 4; i++ ) {
for ( int k = 0; k < 5; k++ ) {
System.out.print(String.format("%15s", jacobsthalOblongNumber(i * 5 + k)));
}
System.out.println();
}
System.out.println();
System.out.println("The first 10 Jacobsthal Primes:");
for ( int i = 0; i < 10; i++ ) {
System.out.println(jacobsthalPrimeNumber(i));
}
}
private static BigInteger jacobsthalNumber(int aIndex) {
BigInteger value = BigInteger.valueOf(parityValue(aIndex));
return BigInteger.ONE.shiftLeft(aIndex).subtract(value).divide(THREE);
}
private static long jacobsthalLucasNumber(int aIndex) {
return ( 1 << aIndex ) + parityValue(aIndex);
}
private static long jacobsthalOblongNumber(int aIndex) {
long nextJacobsthal = jacobsthalNumber(aIndex + 1).longValueExact();
long result = currentJacobsthal * nextJacobsthal;
currentJacobsthal = nextJacobsthal;
return result;
}
private static long jacobsthalPrimeNumber(int aIndex) {
BigInteger candidate = jacobsthalNumber(latestIndex++);
while ( ! candidate.isProbablePrime(CERTAINTY) ) {
candidate = jacobsthalNumber(latestIndex++);
}
return candidate.longValueExact();
}
private static int parityValue(int aIndex) {
return ( aIndex & 1 ) == 0 ? +1 : -1;
}
private static long currentJacobsthal = 0;
private static int latestIndex = 0;
private static final BigInteger THREE = BigInteger.valueOf(3);
private static final int CERTAINTY = 20;
}
You may also check:How to resolve the algorithm Pythagoras tree step by step in the jq programming language
You may also check:How to resolve the algorithm HTTP step by step in the PHP programming language
You may also check:How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Groovy programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the Prolog programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Ring programming language