We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,763 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Program error in data types

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.

4
Contributors
15
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
16
Views
Question
Answered
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 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?

JamesCherrill
... trying to help
Moderator
8,509 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 30

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?

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 22
Skill Endorsements: 3

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

stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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.

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 22
Skill Endorsements: 3

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.

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0

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

JamesCherrill
... trying to help
Moderator
8,509 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 30

mathematically its valid

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 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?

stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 22
Skill Endorsements: 3

itz 11^23

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 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.

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 22
Skill Endorsements: 3

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

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0

can anyone help me to use BigInteger for it?

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 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

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by dantinkakkar, stultuske and JamesCherrill

You're welcome! :)

dantinkakkar
Junior Poster
177 posts since Aug 2011
Reputation Points: 49
Solved Threads: 22
Skill Endorsements: 3

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1328 seconds using 2.78MB