Computer Science Canada

exponents

Author:  cool dude [ Tue Jun 27, 2006 9:06 pm ]
Post subject:  exponents

how would you make an exponent with BigInteger.

i want to get the answer of 2 ^1000 so i need to know how to make this using BigInteger.

Author:  rizzix [ Tue Jun 27, 2006 11:37 pm ]
Post subject: 

Java:
BigInteger result = new BigInteger(2).pow(new BigInteger(1000));

Author:  wtd [ Wed Jun 28, 2006 12:07 am ]
Post subject: 

I can't help myself. Post #4500 is:

code:
(expt 2 1000)


Smile

Author:  rizzix [ Wed Jun 28, 2006 1:36 pm ]
Post subject: 

Haskell:
2 ^ 1000
or
Haskell:
(^) 2 1000

However you like it.

Author:  cool dude [ Wed Jun 28, 2006 7:50 pm ]
Post subject: 

rizzix wrote:
Java:
BigInteger result = new BigInteger(2).pow(new BigInteger(1000));


it says cannot resolve symbol constructor BigInteger(int)

Can someone please make a tutorial on BigInteger! i would be really grateful. as you can tell from all my posts i am not understanding how to use BigInteger. would you rather me post 100 posts about BigInteger

Author:  McKenzie [ Wed Jun 28, 2006 9:17 pm ]
Post subject: 

Two problems.
1. You can't make a BigInteger with an int, use "2" (seems foolish, I agree)
2. The pow does take an int not a BigInteger (you would think it would be overloaded)

Author:  Martin [ Wed Jun 28, 2006 10:13 pm ]
Post subject: 

cool dude wrote:
rizzix wrote:
Java:
BigInteger result = new BigInteger(2).pow(new BigInteger(1000));


it says cannot resolve symbol constructor BigInteger(int)

Can someone please make a tutorial on BigInteger! i would be really grateful. as you can tell from all my posts i am not understanding how to use BigInteger. would you rather me post 100 posts about BigInteger


Well, here's one way you could do it:

Java:
BigInteger result = new BigInteger("2").pow(1000);


The best way to learn how to use it is just to read the API docs (click on the keywords in the syntax highlighted java code).

Author:  rizzix [ Thu Jun 29, 2006 1:22 am ]
Post subject: 

Sorry, my mistake. Yes you have to use a string and pow only accepts ints (primitives).

You don't need a tutorial on BigInteger. The problem here is that you're confusing the BigInteger object with a java primitive. There is a distinction between the two. Objects behave differently and primitives behave differently. If you keep that in mind, you'll make less mistakes.

Secondly, you must take a good look at the method signarurs in the Java API docs. Make sure you are passing the right "typed" argumets to the respective methods. That's all. Smile

Author:  cool dude [ Thu Jun 29, 2006 10:22 am ]
Post subject: 

k thanks. also how would i convert BigInteger to String? i tried this:

code:
String sresult = BigInteger.toString(result);


and how would i get the length of the BigInteger?

Author:  cool dude [ Thu Jun 29, 2006 10:38 am ]
Post subject: 

k i figured out how to convert it to string and how to get the length of that string. the only problem now is when i try and run my program it says:

Quote:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""

...


this is my code:

code:

import java.math.*;
public class Problem16{
        public static void main (String[] args){
                int sum = 0;
                BigInteger result = new BigInteger("2").pow(1000);
                String sresult = result.toString();
                int n = sresult.length();
                for (int i = 1; i < n; i ++){
                        sum = sum + Integer.parseInt((sresult.substring(i,1)));
                }
                System.out.println (sum);
        }
}

Author:  wtd [ Thu Jun 29, 2006 11:20 am ]
Post subject: 

Check out the documentation for the substring method in the String class, and the static parseInt method in the Integer class.

Author:  cool dude [ Thu Jun 29, 2006 9:46 pm ]
Post subject: 

i changed my code a little around and tried different variations but its still the same error! i did read the documentation for the substring method and i know how it works. i also know from other languages and its pretty much the same. i'm not sure where my problem is though. you keep telling me to read the documentations and i do read them but i can not pinpoint the problem.

Here's my newer code which still doesn't work

code:

import java.math.*;
public class Problem16{
        public static void main (String[] args){
                int sum = 0;
                BigInteger result = new BigInteger("2").pow(1000);
                String sresult = result.toString();
               
                for (int i = 0; i < sresult.length(); i ++){
                        sum = sum + Integer.parseInt(sresult.substring(i,1));
                }
                System.out.println (sum);
        }
}

Author:  wtd [ Thu Jun 29, 2006 11:20 pm ]
Post subject: 

I suggested investigating two methods.

Do you know how Integer's static parseInt method works?

Author:  r.3volved [ Fri Jun 30, 2006 9:46 am ]
Post subject: 

First thing I noticed is your for loop. Not much of a problem, but the correct syntax would be:
for(int i = 0; i != sresult.length(); ++i)

it seems to choke when the substring hits a 0 (ZERO)

code:

import java.math.*;
public class Problem16
{
    public static void main (String[] args)
   {
      int sum = 0;
      BigInteger result = new BigInteger("2").pow(1000);
      String sresult = result.toString();
     
      for (int i = 0; i != sresult.length(); ++i)
      {
        sum += Integer.parseInt(sresult.substring(i,i+1));
      }
      System.out.println(sum);
   }
}


Try that...debugging the code killed my netbeans
but I also noticed your substring was incorrect

Author:  cool dude [ Fri Jun 30, 2006 10:04 am ]
Post subject: 

thanks for the help. 2 questions

1) what is the difference between your counted loop and mine? they perform the same thing.

2) i'm now totally confused on the substring because i thought the second digit meant how many letters to take, thus i always put 1. but why i + 1? so if i = 5 then you would take 6 numbers? i only want one number not 6. i don't get how that works Confused

Author:  McKenzie [ Fri Jun 30, 2006 10:20 am ]
Post subject: 

cool dude,

wtd is trying to nudge you in the right direction. Why say you understand substring when clearly you don't. No need to get defensive and talk about the other languages you know. Just go back and look at the API. No big deal.

Author:  r.3volved [ Fri Jun 30, 2006 10:22 am ]
Post subject: 

There's no difference to the for loops. They both do the same thing, but think of it as grammar in english...

You can say:
"How y'all doing today?"
"How is everyone today?"

They are both understandable and lead to the same conclusion but the second is grammatically correct.

You want to go from first character to the last, so the first character is at character array [0] but .length() counts the first character as 1, not 0.
Incrementing by 1 on whole numbers, you know that .length() will be one more than the character array index and the != just avoids off-by-one errors in most cases. ++i and i++ are NOT the same thing. In this case there is no difference, but they both handle the stack differently.

ex.
int i = 1;
int x = 5;

x += i++; //this will add `i` to `x` and then increment `i`
so after execution, `x` will be 6 and `i` will be 2

x += ++i; //this will increment `i` first and then add `i` to `x`
so after execution, `x` will be 7 and `i` will be 2

This often leads to off-by-one errors also (exploit city Smile)
Always remember that the name "C++" is WRONG...should be "++C" based on proper syntax.
----------------------------
As for the substring, the first parameter is the start character and the second parameter is the finish character, not the length. (you must be a c-coder or varient of). Java just does it differently.

If your compiler has active intellisense, make sure you read it well, as each language has it's own qwirks and differences


Hope this helped
Cheers

Author:  Martin [ Fri Jun 30, 2006 11:33 am ]
Post subject: 

substring( int begin, int end ); is the string from begin to end - 1. (begin inclusive to end exclusive).

ie. to get 'mpsc' out of 'compsci', you want to call substring(2,6);

Looks kind of like a lame way to do it. substring(2,5); seems to me like it would be more intuitive.

Author:  wtd [ Fri Jun 30, 2006 11:42 am ]
Post subject: 

r.3volved wrote:
If your compiler has active intellisense, make sure you read it well, as each language has it's own qwirks and differences


As long as you're offering an explanation, let's make sure you get the details right too.

"Intellisense" and other such systems are not a function of a compiler, but rather an IDE. That IDE may include its own particular compiler, but it doesn't need to, and may instead simply use an entirely independent compiler.


: