JDI
JDI

Reputation: 19

Modular Exponentiation Issue in Java

import java.util.Scanner;

class codeabbey145 
{
    public static void main(String[] Args)  
    {
        Scanner input = new Scanner(System.in);
        double A = 0;
        double B = 0;
        double M = 0;
        System.out.println("\n\nHow many sets?");
        int s = input.nextInt();
        double X[] = new double[s];
        for(int i = 0; i<s; i++)
        {
            System.out.println("A: ");
            A = input.nextDouble();
            System.out.println("B: ");
            B = input.nextDouble();
            System.out.println("M: ");
            M = input.nextDouble();
            X[i] = (Math.pow(A, B)) % M;  //(A^B)%M
        }
        for(int j = 0; j<s; j++)
        {
            System.out.print(Math.round(X[j]) + " ");
        }
    }
}

I have been attempting to complete Exercise 145 on Codeabbey.com

The formula given for modular exponentiation is: (A^B)%M

I tried my best to implement this formula into my code, but the answers I have been getting are incorrect. Anybody know why this might be?

Thanks in advance

Upvotes: 0

Views: 493

Answers (2)

rhitz
rhitz

Reputation: 1892

You code is Absolutely correct : Check here .

Probably you should use BigInteger: for handling large numbers , double is never recommended.

Here is the working example: check this live demo

Code

public static void main(String[] Args) {
        Scanner input = new Scanner(System.in);

        System.out.println("\n\nHow many sets?");
        int s = input.nextInt();
        BigInteger[] X = new BigInteger[s];
        for (int i = 0; i < s; i++) {
            System.out.println("A: ");
            BigInteger A = input.nextBigInteger();
            System.out.println("B: ");
            BigInteger B = input.nextBigInteger();
            System.out.println("M: ");
            BigInteger M = input.nextBigInteger();
            X[i] = A.modPow(B, M); //(A^B)%M
        }
        for (int i = 0; i < X.length; i++) {
            System.out.println(X[i]);
        }
    }

I have attempted this question on CodeAbbey. My solution was accepted.

Solution accepted

Code:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main(String[] Args) {
        Scanner input = new Scanner(System.in);
        int s = input.nextInt();
        BigInteger[] X = new BigInteger[s];
        for (int i = 0; i < s; i++) {
            BigInteger A = input.nextBigInteger();
            BigInteger B = input.nextBigInteger();
            BigInteger M = input.nextBigInteger();
            X[i] = A.modPow(B, M);
        }
        for (int i = 0; i < X.length; i++) {
            System.out.println(X[i]+" ");
        }
    }
}

Upvotes: 2

Robby Cornelissen
Robby Cornelissen

Reputation: 97321

Most likely the values are too big to fit into a double, so they overflow. Your best bet is probably to implement using BigInteger objects.

BigInteger has pow and mod functions that you can use for your calculations.

Upvotes: 0

Related Questions