0% found this document useful (0 votes)
9 views3 pages

Josephus Problem Solver in Java

Uploaded by

Nisrine
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Josephus Problem Solver in Java

Uploaded by

Nisrine
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package josephus.

solver;
import [Link];
import [Link];
public class projet_final {

public static void main(String[] args) {


@SuppressWarnings("resource")
Scanner keyboard = new Scanner([Link]);

[Link]("Enter the number of soldiers (>0): ");


int numOfSoldiers = [Link]();
if(numOfSoldiers <= 0) {
do {
[Link]("Please type a valid number");
[Link]("Enter the number of soldiers(>0): ");
numOfSoldiers = [Link]();
} while(numOfSoldiers<=0);
}

[Link]("Enter the jump step(>0): ");


int jump = [Link]();
if(jump <= 0) {
do {
[Link]("Please type a valid number");
[Link]("Enter the jump step(>0): ");
jump = [Link]();
} while(jump <= 0);
}

[Link]("First killer(>=1 and <= %d): ", numOfSoldiers);


int firstkiller = [Link]();
if(firstkiller <= 0 || firstkiller > numOfSoldiers) {
do {
[Link]("Please type a valid number");
[Link]("First killer(>1 and <= %d): ",
numOfSoldiers);
firstkiller = [Link]();
} while(firstkiller <= 0 || firstkiller > numOfSoldiers);
}

[Link]("Which method do you want to use? (1 or 2): ");


int choice = [Link]();

long resultat_1[]= new long [numOfSoldiers+1];


long resultat_2[]= new long [numOfSoldiers+1];

resultat_1 = method1( numOfSoldiers, jump, firstkiller);


resultat_2 = method2( numOfSoldiers, jump, firstkiller);

long time_1=resultat_1[numOfSoldiers] ;
long time_2=resultat_2[numOfSoldiers];
if (choice==1){
for(int i=0; i<numOfSoldiers; i++){
[Link](resultat_1[i]+" ");
}

}
else if (choice==2){
for(int i=0; i<numOfSoldiers; i++){
[Link](resultat_2[i]+" ");
}
}
else [Link] ("sorry you must choose either 1 or 2");

[Link]();
[Link]("the time with the first method in nanosecond is:
"+time_1);
[Link]("the time with the first method in nanosecond is:
"+time_2);
if(time_1<time_2)
[Link](" method 1 is the fastest");
else if(time_1>time_2)
[Link]("method 2 is the fastest");
else [Link]("both methods take the same execution
time");

}
//-------------------------------- Method 1
------------------------------------------------

public static long[] method1(int numOfSoldiers, int jump,int firstkiller){

long start=[Link]() ,k=0;


long Soldiers[] = new long[numOfSoldiers];
long result1[]= new long[numOfSoldiers+1];
int restant=numOfSoldiers, count=0,j=0;

for(int i=0; i<numOfSoldiers; i++){


Soldiers[i]=i+1;
}
while(restant>=1){
for(int i=0; i<numOfSoldiers;i++){
k=Soldiers[i]+firstkiller;
if(Soldiers[i]!=0){
count++;
if(count%jump==0){
Soldiers[i]=0;
restant--;

if (k<=numOfSoldiers) result1[j]=k;
else result1[j]=k-numOfSoldiers;
j++;
result1[numOfSoldiers]=[Link]()- start;
}
}
}
}
return result1;
}
//--------------------------------- Method 2
----------------------------------------------------

public static long[] method2(int numOfSoldiers, int jump,int firstkiller){


int count=0,j=0;
long start=[Link](),k=0;
ArrayList<Integer> Soldiers = new
ArrayList<Integer>(numOfSoldiers);
long result2[]= new long[numOfSoldiers+1];
for(int i=0; i<numOfSoldiers; i++){
[Link](i+1);
}

while([Link]() >= 1){


count = (count + jump - 1)% [Link]();
k= [Link](count) + firstkiller;
if (k<=numOfSoldiers) result2[j]=k;
else result2[j]=k-numOfSoldiers;
j++;
result2[numOfSoldiers]=[Link]()- start;
[Link](count);
}

return result2;
}
}

Common questions

Powered by AI

The `firstkiller` parameter in `method1` adjusts the starting index from which killing commands begin, effectively adding an offset to the base index for selection of each subsequent soldier to be eliminated. It determines initial conditions for both cyclic arrays in `method1` and the starting index calculation in `method2`, as it is added to the calculated position of each soldier slated for execution, modifying how `k`, the next soldier position, is derived .

The execution time for each method is measured using `System.nanoTime()`, capturing the start and end time around the primary logic of each algorithm. By subtracting the start time from the end time, the program calculates the duration in nanoseconds, which is precise enough to measure the efficiency of both methods. This measurement is crucial for a performance evaluation, providing an objective comparison of how each algorithm handles the input parameters under the same conditions .

Choosing an inappropriate first killer index can significantly alter the sequence and result of soldier eliminations, impacting the performance and correctness of the outcome. If the index is outside the valid range, the program forces user correction. An inappropriate choice at the boundary of acceptable range can also increase computational complexity indirectly by necessitating more iterations and affecting the sequence of operations due to disproportionate jump effects, potentially impacting perceived performance due to forcing specific computational paths .

In both methods, the modulo operation is crucial as it ensures the circular nature of the problem is preserved. For `method1`, it helps wrap around the index when determining which soldier to eliminate next, considering the jump interval. For `method2`, it calculates the index directly for removal by using `(count + jump - 1) % Soldiers.size()`, which determines the position of the next soldier to be removed efficiently as the list size changes dynamically .

`Method1` uses a fixed-size array to manage and eliminate soldiers, iterating over the array while keeping track of remaining soldiers and performing modulo operations to determine eliminations. `Method2`, on the other hand, uses an `ArrayList` allowing dynamic resizing. It leverages modulo arithmetic directly on the list to find and remove the next soldier in constant time, simplifying the process of soldier elimination .

`Method1` might outperform `method2` when the overhead associated with dynamic resizing of the `ArrayList` in `method2` becomes significant, particularly with a large number of soldiers. If the jump step is large, resulting in fewer operations on the fixed-size array (where resizing isn't required), `method1` can eliminate soldiers through direct index manipulation, which may become more efficient if memory allocation and garbage collection overheads of `method2` surpass the costs of array operations in `method1` .

The use of an `ArrayList` in `method2` provides dynamic resizing and ease of removal operations, allowing the list to shrink as soldiers are eliminated, which simplifies index management. This contrasts with the static array in `method1` where indices must be managed manually, and marked as '0' when a soldier is eliminated. The `ArrayList` allows direct element removal without leaving gaps, making it functionally simpler when managing dynamically changing lists .

`Projet_final` ensures only valid input for the `jump` parameter by implementing a do-while loop to repeatedly prompt the user until a valid integer greater than zero is entered. This loop continues to prompt the user with 'Please type a valid number' until the condition `jump > 0` is satisfied, similarly ensuring all subsequent operations rely on valid data .

The `projet_final` class provides two different methods to demonstrate alternative algorithmic approaches to the Josephus problem, each with potentially different performance characteristics. Performance is evaluated by measuring and comparing the execution time of each method in nanoseconds. Depending on the execution time results, the program prints which method was faster or if both methods took the same execution time .

The `projet_final` class handles invalid input for the number of soldiers by repeatedly prompting the user until a valid (greater than zero) number is entered. It employs a do-while loop that asks for input until a condition (`numOfSoldiers > 0`) is satisfied, ensuring only valid integers proceed further .

You might also like