-
-
Save dotslash/e6bc1248983d3a34ab98 to your computer and use it in GitHub Desktop.
java snippets
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
package reuse; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.StringTokenizer; | |
final class IO{ | |
//Standard IO | |
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));; | |
static StringTokenizer tokenizer = null; | |
static int ni(){ | |
return Integer.parseInt(ns()); | |
} | |
static long nl(){ | |
return Long.parseLong(ns()); | |
} | |
static double nd(){ | |
return Double.parseDouble(ns()); | |
} | |
static String ns(){ | |
while (tokenizer == null || !tokenizer.hasMoreTokens()) { | |
try { | |
tokenizer = new StringTokenizer(br.readLine()); | |
} catch (IOException e) {} | |
} | |
return tokenizer.nextToken(); | |
} | |
static String nline(){ | |
tokenizer = null; | |
String ret = null; | |
try { | |
ret = br.readLine(); | |
} finally { | |
return ret; | |
} | |
} | |
} |
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
package reuse; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Numbers { | |
public static final long MOD = 1000000007; //10^9 + 7 | |
static ArrayList<Integer> primesUpto(int n) { | |
boolean[] bm = new boolean[n + 1]; //false | |
ArrayList<Integer> primes = new ArrayList<Integer>(); | |
for (int i = 2; i <= n; i++) { | |
if (bm[i]) continue; //not prime | |
primes.add(i); | |
for (long j = (long) i * (long) i; j <= n; j += i) { | |
bm[((int) j)] = true; | |
} | |
} | |
return primes; | |
} | |
static long primeFactorization(long n) { | |
//number of 2s that divide n | |
long ret = 1; | |
long pres = 0; | |
while (n % 2 == 0) { | |
n = n / 2; | |
pres++; | |
} | |
ret = ret * (pres + 1); | |
// n must be odd at this point. So we can skip one element (Note i = i +2) | |
for (int i = 3; i <= Math.sqrt(n); i = i + 2) { | |
// While i divides n, print i and divide n | |
pres = 0; | |
while (n % i == 0) { | |
pres++; | |
n = n / i; | |
} | |
ret = ret * (pres + 1); | |
} | |
// This condition is to handle the case when n is a prime number | |
// greater than 2 | |
if (n > 2) ret = ret * 2; | |
return ret; | |
} | |
static long pow(long base, | |
long exp, | |
long mod) { | |
long ret = 1; | |
long pres = base; | |
while (exp != 0) { | |
if (exp % 2 == 1) ret = (ret * pres) % mod; | |
pres = (pres * pres) % mod; | |
exp = exp / 2; | |
} | |
return ret; | |
} | |
static long pow(long base, long exp) { | |
long ret = 1; | |
long pres = base; | |
while (exp != 0) { | |
if (exp % 2 == 1) ret = (ret * pres); | |
pres = (pres * pres); | |
exp = exp / 2; | |
} | |
return ret; | |
} | |
public static void main(String[] test) { | |
List<Integer> list = primesUpto(100); | |
System.out.println(list.size()); | |
for (int i : list) { | |
System.out.println(i); | |
} | |
System.out.println(pow(2, 13, MOD)); | |
System.out.println(pow(2, 13)); | |
} | |
} |
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
package reuse; | |
/** | |
* Created by ssai | |
* Jun-20-2014. | |
*/ | |
public final class Pair { | |
public final int first; | |
public final int second; | |
public Pair(int first, int second) { | |
this.first = first; | |
this.second = second; | |
} | |
public static Pair of(int first, int second) { | |
return new Pair(first, second); | |
} | |
public Pair setFirst(int newFirst) { | |
return of(newFirst, this.second); | |
} | |
public Pair setSecond(int newSecond) { | |
return of(this.first, newSecond); | |
} | |
@Override | |
public int hashCode() { | |
long hc = ((long) first) * 37 + second; | |
hc = hc % (1000 * 1000 * 1000 + 7); | |
return (int) hc; | |
} | |
@Override | |
public boolean equals(Object oth) { | |
if (!(oth instanceof Pair)) return false; | |
Pair other = (Pair) oth; | |
return (other.first == this.first) && | |
(other.second == this.second); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment