integer pair search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Doegon
    New Member
    • Feb 2008
    • 14

    integer pair search

    guys i'm to write an application program that gets a filename and number form the command line and print all possible pairs of numbers such that a<b< number and (a^2 +b^2 +1)/ab is an integer

    e.g number = 1000
    filename = integers.txt

    output is
    (1,2)
    (2,5)
    (5,13)
    (13,34)
    (34,89)
    (89,233)
    (233,610)

    from this example the integer is always three.but the problem is i can't come with an algorith to produce the output.you help will be much appreciated guys
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    a very simple method is to just loop through each value a=1,2,3,... and b=1,2,3,... and compute the value by given formula. Then see if the result is an integer by comparing
    Code:
    boolean isInteger = (Math.round(result) == result);.
    Or, to avoid float numbers at all,you could calculate
    Code:
     int x= (a * a + b * b +1);
    int y = a * b;
    boolean isInteger = (x % y == 0);
    Originally posted by Doegon
    guys i'm to write an application program that gets a filename and number form the command line and print all possible pairs of numbers such that a<b< number and (a^2 +b^2 +1)/ab is an integer

    e.g number = 1000
    filename = integers.txt

    output is
    (1,2)
    (2,5)
    (5,13)
    (13,34)
    (34,89)
    (89,233)
    (233,610)

    from this example the integer is always three.but the problem is i can't come with an algorith to produce the output.you help will be much appreciated guys

    Comment

    Working...