hello,
i have written the below piece of code for calculating the power function, but when i run the program, it shows the wrong result and i verified it by calculating manually. so please help me to get the correct result.

public class powerFunction
{
	public static void main(String ar[])
	{
		int c = 11;
		int d = 23;
		int n = 187;
		long dt;
		dt = (long)((Math.pow(c,d)) % n);
		System.out.println("dt =" +dt);
	}
}

The output should be 88, but it is displaying 149.

Isn't this fun!! I tried a "manual" calculation in both double and long variables:

void calcDouble() {
      double c=11, cPow2 = c*c, cPow4 = cPow2*cPow2, 
             cPow8 = cPow4*cPow4,cPow16 = cPow8*cPow8, 
             cPow23 = cPow16*cPow4*cPow2*c;
      System.out.println(cPow23 + " " + cPow23%187);     
   }

   void calcLong() {
      long   c=11, cPow2 = c*c, cPow4 = cPow2*cPow2, 
             cPow8 = cPow4*cPow4,cPow16 = cPow8*cPow8, 
             cPow23 = cPow16*cPow4*cPow2*c;
      System.out.println(cPow23 + " " + cPow23%187);     
   }

Which gave the following results:
8.954302432552372E23 175.0
6839173302027254275 65
neither of which match yours.
Personally I'd put my money on the long manual version.
Any other offers?

Funnily...

public class test {
    public static void main(String[] args)
    {
        long hyz = (long) Math.pow(11.0, 23.0);
        hyz = hyz%187;
        System.out.println(hyz);
    }
}

Output = 161?

Also, I edited your code a bit and checked...

int c = 11;
    int d = 23;
    int n = 187;
    long dt;
    dt = (long)((Math.pow(c,d)));
    long rc = dt%n;
    System.out.println(rc);

I put the modulus n part in another line... and whoa! 161

So, you want this, right? (11^23)%187?

when and where you convert to long also has its influence..

Yeah, he's right. Another version of my code got 65 as output. I guess that's what's dwindling your answer... :\

If you try writing a piece of code to find out the exponent of a number, and you try solving 11^23, you get 6839173302027254275.Then, if you try finding out it's remainder with 187, you get 65, which is wrong, I guess, as per you.

listen dears..
if we break (11^23) % 187, then it can written as
{(11^1 % 187) * (11^2 % 187) * (11^4 % 187) * (11^8 % 187) * (11^8 % 187)} % 187
= {11 * 121 * 55 * 33 * 33} % 187
= 88

but programatically, itz not 88. so wHat to do.

commented: as JamesCherill said! -1

No, that's not valid. You must complete the power expression before doing the modulo

mathematically its valid

mathematically its valid

there are forms of math in which

1 + 1 = 1
and
1 + 0 = 0

are valid and correct too, but that's not really what we're looking for, is it?

Back to the topic you get 65 if you write the exponent code yourself, instead of using Math.pow(int,int). @adil_bashir, are you sure it's 11^23 and not 23^11? o.O

itz 11^23

The correct answer is, as you say, 88. The problem - I have recognized it. It's not that we're doing anything wrong with the code. 895430243255237372246531, which is the value of 11^23, is too large a number even for long. :)

It is not computed properly by the compiler. The number is just too big.

that is the title of my problem, so can u please tell me the exact datatype that can be used.

can anyone help me to use BigInteger for it?

Solved by myself..

import java.math.BigInteger;

public class abc3
{
	public static void main(String ar[])
	{
		BigInteger ct = new BigInteger("11");
		BigInteger n = new BigInteger("187");
		BigInteger d = new BigInteger("23");
		BigInteger e  = new BigInteger("7");     
                BigInteger decr = ct.modPow(d,n);
		System.out.println("Decr = "+decr);
	}
}

and now the output is 88.

thanks all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.