How to make a program do some maths!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hazza
    New Member
    • Oct 2007
    • 22

    #1

    How to make a program do some maths!

    Hello, I am trying to write a program to solve a simultaneous equation. It runs through all the possibilites for x, and if any of them forfil the conditions of y, then it is displayed. My problem is that i don't know how to make it so that only positive integers are displayed. What I have done was change a double value integer, and then display that, but all it does it cut of the decimals, not give me all the possibles for x and y that are whole numbers without being rounded.
    Here is my code
    [CODE]
    class Maths {
    public static void main (String[] args) {
    for (int y = 10; y <= 92; y++){
    double a = Math.sqrt((y*y) +180);
    int x = (int)a;
    System.out.prin tln(x + " " + y + " ");
    }
    }
    }
    [CODE]

    Thanks to anyone who helps
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Hazza
    Hello, I am trying to write a program to solve a simultaneous equation. It runs through all the possibilites for x, and if any of them forfil the conditions of y, then it is displayed. My problem is that i don't know how to make it so that only positive integers are displayed. What I have done was change a double value integer, and then display that, but all it does it cut of the decimals, not give me all the possibles for x and y that are whole numbers without being rounded.
    Here is my code
    [CODE]
    class Maths {
    public static void main (String[] args) {
    for (int y = 10; y <= 92; y++){
    double a = Math.sqrt((y*y) +180);
    int x = (int)a;
    System.out.prin tln(x + " " + y + " ");
    }
    }
    }
    [CODE]

    Thanks to anyone who helps
    Write an isInteger method that takes a double.
    Hint: If you call Integer.parseIn t with a string that does not represent an integer, you get a NumberFormatExc eption.

    Comment

    • Hazza
      New Member
      • Oct 2007
      • 22

      #3
      Originally posted by r035198x
      Write an isInteger method that takes a double.
      Hint: If you call Integer.parseIn t with a string that does not represent an integer, you get a NumberFormatExc eption.
      Thanks - sorry to ask, but as i'm new to java (started a week ago) could you show me the code and where to put it?
      Thanks for your help

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Hazza
        Thanks - sorry to ask, but as i'm new to java (started a week ago) could you show me the code and where to put it?
        Thanks for your help
        Why don't you try to write it first like I said. Just a static method in your class that takes a double as argument and returns a boolean.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Write an isInteger method that takes a double.
          Hint: If you call Integer.parseIn t with a string that does not represent an integer, you get a NumberFormatExc eption.
          1.0 does represent an int but still a NFE is thrown ...

          kind regards,

          Jos

          Comment

          • Hazza
            New Member
            • Oct 2007
            • 22

            #6
            Originally posted by r035198x
            Why don't you try to write it first like I said. Just a static method in your class that takes a double as argument and returns a boolean.
            Sorry but I don't understand what a static method is.
            Thanks for your help

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              1.0 does represent an int but still a NFE is thrown ...

              kind regards,

              Jos
              Yuck.
              @OP You should forget about the hint I posted above which is hopelessly incorrect.
              Instead you should split the double on the comma and test that the right most string is equivalent to zero.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                Yuck.
                @OP You should forget about the hint I posted above which is hopelessly incorrect.
                Instead you should split the double on the comma and test that the right most string is equivalent to zero.
                Math.floor() or Math.ceil() can do the job too you know ;-)

                kind regards,

                Jos

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  I gave it a few more thoughts: the problem is that 180+y.y must be an integer
                  square. y must be a positive integer as well. Let that square be (y+x).(y+x) so

                  180+y.y == (y+x).(y+x) --->
                  180+y.y == y.y + 2.y.x + x.x -->
                  180 == 2.y.x+ x.x -->
                  180 == x.(x+2.y)

                  The prime divisors of 180 are 2, 2, 3, 3 and 5. so both x and y need to be even
                  but not a multiple of 4 (there are only 2 prime divisors equal to 2 in 180)

                  The following positive values are valid:

                  x == 2 --> 2.(2+2.44)
                  x == 6 --> 6.(6+2.12)
                  x == 10 --> 10.(10+2.4)

                  So for y equal to 4, 12 and 44 there are integer solutions.

                  kind regards,

                  Jos (<--- look ma! no computer needed! ;-)

                  Comment

                  • Hazza
                    New Member
                    • Oct 2007
                    • 22

                    #10
                    Originally posted by JosAH
                    I gave it a few more thoughts: the problem is that 180+y.y must be an integer
                    square. y must be a positive integer as well. Let that square be (y+x).(y+x) so

                    180+y.y == (y+x).(y+x) --->
                    180+y.y == y.y + 2.y.x + x.x -->
                    180 == 2.y.x+ x.x -->
                    180 == x.(x+2.y)

                    The prime divisors of 180 are 2, 2, 3, 3 and 5. so both x and y need to be even
                    but not a multiple of 4 (there are only 2 prime divisors equal to 2 in 180)

                    The following positive values are valid:

                    x == 2 --> 2.(2+2.44)
                    x == 6 --> 6.(6+2.12)
                    x == 10 --> 10.(10+2.4)

                    So for y equal to 4, 12 and 44 there are integer solutions.

                    kind regards,

                    Jos (<--- look ma! no computer needed! ;-)
                    Thanks a lot guys - especially Jos

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by JosAH
                      I gave it a few more thoughts: the problem is that 180+y.y must be an integer
                      square. y must be a positive integer as well. Let that square be (y+x).(y+x) so

                      180+y.y == (y+x).(y+x) --->
                      180+y.y == y.y + 2.y.x + x.x -->
                      180 == 2.y.x+ x.x -->
                      180 == x.(x+2.y)

                      The prime divisors of 180 are 2, 2, 3, 3 and 5. so both x and y need to be even
                      but not a multiple of 4 (there are only 2 prime divisors equal to 2 in 180)

                      The following positive values are valid:

                      x == 2 --> 2.(2+2.44)
                      x == 6 --> 6.(6+2.12)
                      x == 10 --> 10.(10+2.4)

                      So for y equal to 4, 12 and 44 there are integer solutions.

                      kind regards,

                      Jos (<--- look ma! no computer needed! ;-)
                      No chocolates for such elementary work though.
                      Maybe just one small piece for KISSing it with Math.floor ...

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by r035198x
                        No chocolates for such elementary work though.
                        Maybe just one small piece for KISSing it with Math.floor ...
                        You didn't see any source code from me so I'm innocent so all the chocolates
                        are mine; all mine! and a beer ... its 17:00 now so I'm entitled to have a beer now.
                        And the chocolates ;-)

                        kind regards,

                        Jos

                        Comment

                        Working...