×

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below.

The game begins with an integer num, used to create a unique integer from 1 to num, inclusive (i.e., ).

Set s=1, 2,... (num-1), number.

The two players alternate turns, while Alice always has the first turn.

The current player selects a prime number num from the set S for each move. After that, the player eliminates all of its multiples.

The game is won by the first person who is unable to move.

Bob and Alice had some fun. Print the name of the game's winner on a new line using the value for each game. Print Alice if she triumphs; print Bob if she loses.

Note: Each participant always plays optimally, which means they won't take a step that puts them at a disadvantage if there's a more advantageous one.

Input

The number of games Alice and Bob play is indicated by the integer G, which appears on the first line.

The next lines each include a single number, G, which describes a game, and Num describes a game.

Output

The output should display the winner of the game.

Java Program for Alice and bob

Filename: AliceBob.java

//Program for Alice and bob in Java
//importing the required packages
import java.io.*;
//package for reading the dynamic input from the user
import java. util.*;
public class AliceBob
{
    static String winners(int num)
    {
        //the condition for the primes
        boolean isPrimes[] = new boolean[num + 1];
        Arrays.fill(isPrimes , true);
        int counts = 0;
        //the initial values are declared
        isPrimes[0] = false;
        isPrimes[1] = false;
        //loop for iterating over all possibilities from 2 to num
        for(int i = 2; i*i <= num; i++)
        {
            for(int j = 2*i; j <= num; j += i)
            {
                isPrimes[j] = false;
            }
        }
        for(int i = 0; i <= num; i++)
        {
            //If the value is prime, then the count's variable value increases
            if(isPrimes[i])
            {
                counts++;
            }
        }
        //If the result obtained is divisible by 2;
     // then it will return to "Bob". Else, “Alice."
        if(counts % 2 == 0)
        {
            return "Bob";
        }
        else
        {
            return "Alice";
        }
    }
    //main section of the program
    public static void main(String[] args)
    {
        //the method for reading the dynamic values
        Scanner sca =  new Scanner(System.in);
        int totalgames = sca.nextInt();
        //loop will iterate over all games
        for(int i = 0; i < totalgames; i++)
        {
            int num = sca.nextInt();
            //display the result of the winner of the game
            System.out.println("The winner of the game is: "+winners(num));
        }
    }
}

Output

4
23
The winner of the game is: Alice
12
The winner of the game is: Alice
1
The winner of the game is: Bob
78
The winner of the game is: Alice

Related Topics

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Self-Descriptive Numbers in Java

A number n is given. Identifying the self-descriptive numbers that exist between 1 and n is our task. Self-Descriptive Numbers The definition of a self-descriptive number, m, is a number with b...

6 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Conditional operator in Java

In Java, there are around eight operators, and among them, three operators are used to evaluate the condition and decide the Result based on the Result of the evaluated condition. Below...

4 minutes read.