×

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

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Java BufferedWriter

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.