简体   繁体   中英

Generate an infinite stream of tuples

I need to generate an infinite stream of tuples, which satisfy the equation:

2 * a * a + b * b = c * c

I am just starting with java 8 and am unsure how to achieve this. I have an interface for the tuple:

public interface Tuple {
    /**
     * @return The value of A
     */
    int getA();

    /**
     * @return The value of B
     */
    int getB();

    /**
     * @return The value of C
     */
    int getC();
}

And so far I have this method:

public static Stream<Tuple> generateABCTuples() {
    Supplier<Tuple> aTuple = (Supplier<Tuple>) generateABCTuples();
    Stream<Tuple> myList = Stream.generate(aTuple)
                                 .sorted();
    return myList;
}

However, i am unsure how to satisfy the above equation. Any help on this is much appreciated.

Let's do a little research (using non-negative values)
2*a^2+b^2=c^2
2*a^2 = c^2 - b^2 = (cb)*(c+b)
We can see that b and c must be both odd or both even. Anyway, right part is divisible by 4, so left is divisible by 4 too, and a is even. Another condition: c >= b
Let's

a = 2*p
u=(c-b)/2
v=(c+b)/2 [with v>=u]     

so

b=v-u  
c=v+u
8*p^2 = 4 * u * v
2*p^2 = u * v  

Now we can take any value of p, factorize 2*p^2, find possible factors u and v, and get corresponding a, b, c values (probably not unique). Example:

p=0  =>  u=0, v=any value, all c=b pairs are the solutions 2*0+k^2=k^2
p=1  =>  v=2, u=1  a=2 b=1 c=3   2*4+1=9  
p=2  =>  v=8, u=1  a=4 b=7 c=9   2*16+49=81  //and the second factorization:
         v=4, u=2  a=4 b=2 c=6    2*16+4=36
and so on...

Of course, every triplet element might be negative: a=-2 b=1 c=-3 is valid solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM