Results 1 to 11 of 11

Thread: Powes of 7

  1. #1
    himanwish is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default Powes of 7

    Hi guys,

    I need to wirte a program which displays the powers of 7 from 0 to 35.

    I tried using a for loop and also Math.pow(7,i) ( i is numbers from 0 to 35, inclusive 0 and 35). But cannot get the proper output ( I can get the output from 0 to 20, but after that it doesn't look happy).

    What is the best thing to do?

    Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by himanwish View Post
    What is the best thing to do?
    Post your code so we can see what you have done.

  3. #3
    himanwish is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the reply. The code is as below-

    answer 1-
    public class PowersOfSeven {

    public static void main (String [] args) {

    int power = 1;

    for (int count = 0; count <= 35; count++){

    System.out.println ("7 ^ " + count + " = " + power);

    power = power * 7;

    }

    }
    }


    Answer 2-

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Your problem is that after a while the value of power will be greater than Integer.MAX_VALUE and therefore wraps around to a negative value.

    You may want to check with your teacher. I thought the assignment was asking for multiples of seven. ie
    7
    14
    21
    28
    35

  5. #5
    himanwish is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    Thanks your help again.

    It's definitely about powers of 7. The program should print out like below-

    7 ^ 0 = 1
    7 ^ 1 = 7
    7 ^ 2 = 49
    ..............
    .............
    .............
    .............
    7 ^ 35 =

    up to 7 ^ 20 I think program runs ok , but after that it doesn't loook right.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look into large number types such as long, double and BigInteger. Like junky said, the value is reaching the max int level and overflowing into the negative range.

    Integers are only 32 bits and they are signed so the max is 2^31 and -2^31, which is approximately 2.1 bill( I could be off a bit, not around a computer to test it)
    Last edited by sunde887; 03-04-2011 at 01:59 PM. Reason: Edited to correct min and max int bit representation

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

  8. #8
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    Look into large number types such as long, double and BigInteger. Like junky said, the value is reaching the max int level and overflowing into the negative range.

    Integers are only 32 bits and they are signed so the max is 2^15 and -2^15, which is approximately 2.1 bill( I could be off a bit, not around a computer to test it)
    2^15 is 32768; the sign doesn't take up 17 bits. The max and min int values are (2^31)-1 and -(2^31) respectively.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thanks for catching that, was typing on my phone and rushing

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by himanwish View Post
    Thanks your help again.

    It's definitely about powers of 7. The program should print out like below-

    7 ^ 0 = 1
    7 ^ 1 = 7
    7 ^ 2 = 49
    ..............
    .............
    .............
    .............
    7 ^ 35 =

    up to 7 ^ 20 I think program runs ok , but after that it doesn't loook right.
    Time to switch on your rusty old calculator: press 7 log 10 log / * 10; the answer tells you how many digits are needed to store 7 raised to the power 35. That number is way too large to store in an int. Use BigDecimal number type objects for the calculations.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    himanwish is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •