Skip to content

Instantly share code, notes, and snippets.

@Ramasubramanian
Ramasubramanian / euler-p8.clj
Created July 24, 2011 16:10
Euler problem 8 in Clojure
(defn euler-8
"Problem 8"
([numstr]
(euler-8 (map #(Character/getNumericValue %) numstr) 0))
([numseq prod]
(let [temp-prod (reduce * (take 5 numseq))
next-list (rest numseq)]
(if (< (count next-list) 5) prod
(recur next-list
(if (> temp-prod prod) temp-prod prod))))))
@Ramasubramanian
Ramasubramanian / euler-p10.clj
Created July 24, 2011 16:13
Euler problem 10 in Clojure
(defn sieve-of-era
"Generate the primes upto N based on sieve of erastothenes"
([num] (sieve-of-era [] num (range 2 num)))
([primes num sieve]
(let [x (first sieve)]
(def divisible? (fn [divisor num] (zero? (rem num divisor))))
(def remaining (fn [coll x] (remove (partial divisible? x) coll)))
;optimisation - enough to remove until current prime ^ 2 < limit
(if (>= (* x x) num) (concat primes sieve)
(recur (conj primes x) num (remaining (rest sieve) x))))))
@Ramasubramanian
Ramasubramanian / euler-p11.clj
Created July 24, 2011 16:17
Euler problem 11 in Clojure
;Project Euler problem 11 - find the greatest product of 4 elements in the 20*20
;grid either vertically, horizontally or diagonally
(def grid
[[8 2 22 97 38 15 0 40 0 75 4 5 7 78 52 12 50 77 91 8]
[49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 4 56 62 0]
[81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 3 49 13 36 65]
[52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 2 36 91]
[22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80]
[24 47 32 60 99 3 45 2 44 75 33 53 78 36 84 20 35 17 12 50]
[32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70]
@Ramasubramanian
Ramasubramanian / redirect.java
Created September 6, 2011 15:29
Frame bust redirect
public void redirect(final HttpServletResponse response) throws IOException{
String url = "http://abcde.com";
String out = "<script type=\"text/javascript\">window.top.location=\"" + url+ "\"</script>";
response.getWriter().print(out);
response.flushBuffer();
}
public synchronized int hashCode() {
if (hashCode != -1)
return hashCode;
hashCode = handler.hashCode(this);
return hashCode;
}
@Ramasubramanian
Ramasubramanian / handler_hash.java
Created October 3, 2011 15:51
handler hash code
protected synchronized InetAddress getHostAddress(URL u) {
if (u.hostAddress != null)
return u.hostAddress;
String host = u.getHost();
if (host == null || host.equals("")) {
return null;
} else {
try {
u.hostAddress = InetAddress.getByName(host);
private static InetAddress[] checkLookupTable(String host) {
synchronized (lookupTable) {
// If the host isn't in the lookupTable, add it in the
// lookuptable and return null. The caller should do
// the lookup.
if (lookupTable.containsKey(host) == false) {
lookupTable.put(host, null);
return null;
}
@Ramasubramanian
Ramasubramanian / SatvimPuzzle.java
Created October 11, 2011 07:52
Naive solution to Satvim's puzzle
package test_crawler.test;
import java.util.ArrayList;
import java.util.List;
/**
* 1. Create a 2D array of characters
* 2. Gather the lower edge indexes first e.g. in a 3 x 3 array gather all y positions alone
* y x x
@Ramasubramanian
Ramasubramanian / ObjectSizeCalcTest.java
Created February 2, 2012 17:34
Object size calculation in Java
import java.util.ArrayList;
import java.util.List;
public class ObjectSizeCalcTest {
static class Employee {
public int age;
public int id;
public String name;
public Employee(int age, int id, String name) {
@Ramasubramanian
Ramasubramanian / FileUtil.java
Created February 9, 2012 14:45
File helper class
import java.io.File;
import java.io.FilenameFilter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class FileUtil {
private static String getSimpleFileName(final String fileName){
if(fileName == null || fileName.length() == 0)