-2
Sum(N) =1^1+2^2+3^3+...+N^N

Using Java, How would I use BigInteger to find the smallest integer N such that the value of Sum(N) is larger than 10^20? I'm really stuck,please give me some advice

This is what I have so far:

import java.math.BigInteger;
public class PROJECTV1 {
    public static void main(String [] args) {
        BigInteger bResult= bigFunctionExample_2();
        System.out.println(" => result_got:"+ bResult);
        System.out.println(); //newline
    }// end_main 

    public static BigInteger bigFunctionExample_2() {
        BigInteger bSum = BigInteger.ZERO;
        BigInteger bTmp;
        String sSum;
        // BigInteger bResult =0;
        for (int i=1; ; i++) {
            bTmp = BigInteger.valueOf(i);
            bTmp = bTmp.pow(i); // i^i
            bSum = bSum.add(bTmp); // sum = i^i+ (i-1)^(i-1)+ ....
            sSum = bSum.toString();
            if ( sSum.length() >21) {
                System.out.println("i="+i +" bSum ="+bSum);
                break;
            }//
        }//end_for
        return bSum; // result
    } // end_bigFunctionExample_2
}
  • 2
    its easy man, a double for-loop would do the trick, but atleast try something on your own – Sarthak Mittal Nov 24 '14 at 19:11
  • A first step may be to first printOut arbitrary outputs, IE calc and show – Caffeinated Nov 24 '14 at 19:11
  • Well, the first step is to write a `public static void main(String[] args)` method... oh, what, you've done that already? Well how would I know unless you show us where it is you're stuck? – dcsohl Nov 24 '14 at 19:18
  • 2
    How would you do this if you had to do it yourself with pencil and paper? (And change `10^20` to a much smaller number like 100 if that helps you think about it.) Figure out how you'd do that, then find a way to translate that into code. And if there's one particular step you're having trouble translating, check the Javadoc first. If you do that and you're still stuck, then ask us. – ajb Nov 24 '14 at 19:26
  • Thank you all guys for the advices,I'm really new to this forum,sorry I shouldn't have just asked for the codes.I somehow made it this far – Anthony Hsieh Nov 24 '14 at 20:45
  • import java.math.BigInteger ; public class PROJECTV1 { public static void main(String [] args) { BigInteger bResult= bigFunctionExample_2(); System.out.println(" => result_got:"+ bResult); System.out.println() ; //newline }// end_main – Anthony Hsieh Nov 24 '14 at 20:46
  • public static BigInteger bigFunctionExample_2() { BigInteger bSum = BigInteger.ZERO; BigInteger bTmp; String sSum; // BigInteger bResult =0; for (int i=1; ; i++) { bTmp = BigInteger.valueOf(i); bTmp = bTmp.pow(2); // i^2 bSum = bSum.add(bTmp); // sum = i^2+ (i-1)^2 + .... sSum = bSum.toString(); if ( sSum.length() >30) { System.out.println("i="+i +" bSum ="+bSum); break; }// }//end_for return bSum; // result }// end_bigFunctionExample_2 } – Anthony Hsieh Nov 24 '14 at 20:47
  • It's running extremely long and it hasn't returned a value back,am I doing it right? – Anthony Hsieh Nov 24 '14 at 20:48
  • I'm a newbie here.Can you instruct me how to post codes in a proper way,sorry for the inconvenience – Anthony Hsieh Nov 24 '14 at 21:20

2 Answers2

0

Here is a clue computing some factorials:

import java.math.*;

public class FactorialBig {
    public static BigInteger factorial(BigInteger n) {
        if (n.equals(BigInteger.ZERO))
            return BigInteger.ONE;
        else
            return n.multiply(factorial(n.subtract(BigInteger.ONE)));
    }
    public static void main(String[] args) {
        for (int n = 0; n < 20; n++) {
            BigInteger f = factorial(new BigInteger(new Integer(n).toString()));
            System.out.printf("factorial(%2d) = %20s%n", n, f.toString());
        }
    }
}

You know you should save the above as a file named "FacotrialBig.java".

Simon Woo
  • 474
  • 3
  • 5
0

Looking at your code, you have a line bTmp.pow(2). That squares the numbers in your series, but you need to raise bTmp to the bTmp power. Java doesn’t seem to want to take a BigInteger as an argument to pow, but you could replace pow with another for loop.

Also, sSum.length() >30 looks like it will only occur if your sum is greater than or equal to 1029. Is there a reason you convert the number to a string each time through the loop, rather than comparing the number to 1020? Perhaps you could put something like bSum > bMax as the test condition in your for loop, rather than leaving it blank and exiting with a break. Then you could make a new BigInteger bMax and set it to 1020 at the start of your code.

For testing, you could set bMax to something small, like 100, and see if your program gives the correct result. You can calculate the first few steps of the series by hand to check your program.

yellowantphil
  • 1,483
  • 5
  • 21
  • 30
  • I changed the power of bTmp to i and compare whether sSum has more than 21digits(which means it is larger than 10^20). I now have an answer,I will later try it with smaller values just as you suggested.Thank you so much! – Anthony Hsieh Nov 25 '14 at 02:34
  • @AnthonyHsieh Oh right, that's a better idea. I hope it works now. – yellowantphil Nov 25 '14 at 02:38
  • yup,and the value is extremely large(of course).I don't have my computer now.I will test the program with smaller values to insure there are no flaws.Thanks for the advices :) – Anthony Hsieh Nov 25 '14 at 02:40