Skip to content

Instantly share code, notes, and snippets.

@jabrena
Created December 30, 2021 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabrena/3a01fb3cba82d2e925c885b5c787b947 to your computer and use it in GitHub Desktop.
Save jabrena/3a01fb3cba82d2e925c885b5c787b947 to your computer and use it in GitHub Desktop.
1/3 = 1/4 + 1/4^2 + 1/4^3 + 1/4^N
import java.math.BigInteger;
import java.util.stream.Stream;
public class ShiftDemo {
public static void main(String[] args) {
System.out.println("Hello, World!");
int bignr = 1146423427;
int widthdiv = bignr / 3;
//1/3 = 1/4 + 1/4^2 + 1/4^3 + 1/4^N
var divless = Stream.iterate(1, i -> i + 1) //Infinite Stream
.limit(40)
.filter(n -> (n % 2 == 0 ) ? true : false)
.map(n -> BigInteger.valueOf(bignr).shiftRight(n))
.reduce(BigInteger.ZERO, BigInteger::add);
System.out.println(widthdiv);
System.out.println(divless);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment