java - Generate an infinite stream of tuples -
i need generate infinite stream of tuples, satisfy equation:
2 * * + b * b = c * c
i starting java 8 , unsure how achieve this. have interface tuple:
public interface tuple { /** * @return value of */ int geta(); /** * @return value of b */ int getb(); /** * @return value of c */ int getc(); } and far have method:
public static stream<tuple> generateabctuples() { supplier<tuple> atuple = (supplier<tuple>) generateabctuples(); stream<tuple> mylist = stream.generate(atuple) .sorted(); return mylist; } however, unsure how satisfy above equation. on appreciated.
let's little research (using non-negative values)
2*a^2+b^2=c^2
2*a^2 = c^2 - b^2 = (c-b)*(c+b)
can see b , c must both odd or both even. anyway, right part divisible 4, left divisible 4 too, , even. 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 can take value of p, factorize 2*p^2, find possible factors u , v, , corresponding a, b, c values (probably not unique). example:
p=0 => u=0, v=any value, c=b pairs 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 second factorization: v=4, u=2 a=4 b=2 c=6 2*16+4=36 , on... of course, every triplet element might negative: a=-2 b=1 c=-3 valid solution
Comments
Post a Comment