1,105,594 Community Members

Program error in data types

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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?

Member Avatar
dantinkakkar
Junior Poster
174 posts since Aug 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

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?

Member Avatar
stultuske
Posting Expert
5,332 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Member Avatar
dantinkakkar
Junior Poster
174 posts since Aug 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

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.

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
-1
 

mathematically its valid

Member Avatar
stultuske
Posting Expert
5,332 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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?

Member Avatar
dantinkakkar
Junior Poster
174 posts since Aug 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

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

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

itz 11^23

Member Avatar
dantinkakkar
Junior Poster
174 posts since Aug 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

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.

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

can anyone help me to use BigInteger for it?

Member Avatar
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: -7 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 2 Years Ago by dantinkakkar, stultuske and JamesCherrill
Member Avatar
dantinkakkar
Junior Poster
174 posts since Aug 2011
Reputation Points: 19 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
0
 

You're welcome! :)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article