简体   繁体   English

生成无限元组

[英]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 2 * a * a + b * b = c * c

I am just starting with java 8 and am unsure how to achieve this. 我只是从Java 8开始,不确定如何实现这一点。 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. 我们可以看到b和c必须均为奇数或偶数。 Anyway, right part is divisible by 4, so left is divisible by 4 too, and a is even. 无论如何,右部分可以被4整除,因此左部分也可以被4整除,并且a是偶数。 Another condition: c >= b 另一个条件: 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). 现在我们可以取p的任何值,将2 * p ^ 2分解,找到可能的因子u和v,并获得相应的a,b,c值(可能不是唯一的)。 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 当然,每个三元组元素都可能为负: a=-2 b=1 c=-3是有效的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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