Created
March 1, 2017 17:46
-
-
Save zelinskiy/cac06f9b25013ba8c7489a0b5e763245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.Supplier; | |
import java.util.stream.Stream; | |
import java.awt.font.NumericShaper.Range; | |
import java.util.*; | |
public final class Main { | |
public static int gcd(int a, int b){ | |
if(b == 0) return a; | |
return gcd(b, a%b); | |
} | |
public static int getDigitSum(int x){ | |
int res = 0; | |
for(int n = 0; n < Math.log10(x); n++){ | |
int p0 = (int)Math.pow(10, n); | |
int p1 = (int)Math.pow(10, n+1); | |
res += ((x % p1) - (x % p0)) / p0; | |
} | |
return res; | |
} | |
public static boolean fermatTest(int n){ | |
Random R = new Random(); | |
int a = R.nextInt(); | |
while(a % n == 0){ | |
a = R.nextInt(); | |
} | |
return Math.pow(a, n-1) == 1 % n; | |
} | |
public static boolean isPrime(int x) { | |
if(x == 1) return false; | |
if(x == 2) return true; | |
if (x % 2 == 0) | |
return false; | |
for (int d = 3; d <= (int) Math.sqrt(x); d++){ | |
if (x % d == 0) return false; | |
} | |
return true; | |
} | |
public static int randRange(int a, int b){ | |
return a + (int)(Math.random() * ((b - a) + 1)); | |
} | |
public static int sfact(int n){ | |
int r = 1; | |
for(int i=2; i<=n; i++){ | |
r *= i; | |
} | |
return r; | |
} | |
public static int[] memo = new int[1000000]; | |
public static int fact(int n){ | |
if(n == 1){ | |
return 1; | |
} | |
if(memo[n-1] != 0){ | |
return memo[n-1]; | |
} | |
int res = 1; | |
if(n > 0){ | |
res = n * fact(n-1); | |
} | |
memo[n-1] = res; | |
return res; | |
} | |
public static int factSeq(int n){ | |
int r = 0; | |
for(int s = 1, i=1; i<=n; i++, s*=(-1)){ | |
r += s*fact(i); | |
} | |
return r; | |
} | |
public static boolean isHappy2(int n){ | |
int nd = (int)Math.ceil(Math.log10(n))/2; | |
int rho = (int)Math.pow(10, nd); | |
int a = n / rho; | |
int b = n % rho; | |
return getDigitSum(a) == getDigitSum(b); | |
} | |
public static boolean isHappy3(int n){ | |
int nd = (int)Math.ceil(Math.log10(n))/2; | |
int rho = (int)Math.pow(10, nd); | |
int a = n / (rho*10); | |
int b = n % rho; | |
return getDigitSum(a) == getDigitSum(b); | |
} | |
public static void showHappy(int n){ | |
if(n <= 1) return; | |
if(n%2 == 0){ | |
for(int i = (int)Math.pow(10, n-1)+1; | |
i< (int)Math.pow(10, n) - 1; | |
i++){ | |
if(isHappy2(i)){ | |
System.out.println(i); | |
} | |
} | |
} | |
else{ | |
for(int i = (int)Math.pow(10, n-1)+1; | |
i< (int)Math.pow(10, n) - 1; | |
i++){ | |
if(isHappy3(i)){ | |
System.out.println(i); | |
} | |
} | |
} | |
} | |
public static int[] fibArray(int n){ | |
int[] R = new int[n+1]; | |
R[0] = 1; | |
R[1] = 1; | |
for(int i = 2; i <= n; i++){ | |
R[i] = R[i-1] + R[i-2]; | |
} | |
for(int i=0;i<n;i++){ | |
System.out.println(R[i]); | |
} | |
return R; | |
} | |
public static int[] primeArray(int n){ | |
int[] R = new int[n]; | |
int i = 0; | |
int curr = 0; | |
while(i != n-1){ | |
if(isPrime(curr)){ | |
R[i] = curr; | |
System.out.println(curr); | |
i++; | |
} | |
curr++; | |
} | |
return R; | |
} | |
public static char opp(char c){ | |
if(c == 'B') return 'A'; | |
return 'B'; | |
} | |
public static char[][] chessArray(int n, int m){ | |
char[][] R = new char[n][m]; | |
//will be switched to opposite | |
char c = 'B'; | |
for(int i = 0; i<n; i++){ | |
for(int j = 0; j < m; j++){ | |
c = opp(c); | |
R[i][j] = c; | |
System.out.print(c); | |
} | |
c = opp(c); | |
System.out.print('\n'); | |
} | |
return R; | |
} | |
public static void main(String[] args) { | |
chessArray(10,20); | |
//System.out.println(getDigitSum(123456)); | |
System.out.println("DONE"); | |
} | |
private static void lol(){ | |
Stream<Integer> list = | |
new ArrayList<>( | |
Arrays.asList( | |
1 | |
,2 | |
,3 | |
,13 | |
,20 | |
)).stream(); | |
//Supplier<Stream<Integer>> listSource = () -> Stream.of(list); | |
Integer res = list | |
.filter((Integer i) -> i < 10) | |
.map((Integer i) -> i*i) | |
.reduce(0,((Integer a, Integer b) -> a + b)); | |
list.forEach((Integer i) -> System.out.println(i)); | |
System.out.println(res); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment