Skip to content

Instantly share code, notes, and snippets.

def root = new File(".")
def depth = 1
root.traverse (
type: groovy.io.FileType.DIRECTORIES,
preDir: { depth++ },
postDir: { depth-- },
) { dir ->
println "\t"*depth+dir.name
}
import java.io.File
import groovy.xml.MarkupBuilder
abstract class Entry {
/** parent documentor, used to access doc various formats */
def doc;
/** node name */
def name;
/** Comments to be kept at last */
import groovyx.gpars.*
import groovy.util.GroovyCollections
@Grab(group="org.codehaus.gpars", module="gpars", version="0.11")
def getMatchingCombos(target) {
combos = [200, 100 , 50, 20, 10, 5, 2, 1]
// Using sequences is far too time-consuming (brute-force is never the best option, contrary to Jeff Artwood thoughs ;-) )
// def sequences = GroovyCollections.subsequences(combos)
GParsPool.withPool(2) { pool ->
combos = combos.collectParallel { n ->
import groovyx.gpars.GParsPool
/**
* Extract decimals of n in correct order
*/
def decimals(int n) {
def returned = []
while(n>0) {
returned << n%10
n/=10
long start = System.currentTimeMillis();
def range = 1..501
println range.sum(-3, { n -> 4*(2*n-1)*(2*n-1)-12*(n-1) })
long end = System.currentTimeMillis();
println "duration "+((end-start)/1000.0)+" s";
// courtesy of http://www.nearinfinity.com/blogs/seth_schroeder/project_euler_problem_10_meets.html and others
def primes(upto) {
def primes = []
def range = 2..upto
for(i in range) {
primes[i] = true
}
primes.remove(0)
for(def index=2; index<upto; index++) {
if(primes[index]) {
import org.jasypt.util.text.*;
@Grab(group="org.jasypt", module="jasypt", version="1.7")
String decrypt(String text, String password) {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
encryptor.setPassword(password);
return encryptor.decrypt(text);
}
// An offset defining a kind of header we don't want to see as it pollutes computations
// Yup, it's a hack
def OFFSET = 1000
def SCANNED_SIZE = 10000
def UPSCALE = SCANNED_SIZE+OFFSET
def MAX = 1000
long start = System.currentTimeMillis();
def dividorsOf(number) {
def returned = new TreeSet()
def max = Math.sqrt(number).longValue()+1L
def range = max..1L
for(factor in range) {
if(!returned.contains(factor)) {
if(number%factor==0) {
if(factor<number)
returned << factor
def quotient = (number/factor).longValue()
def dividorsOf(existingDividers, number) {
def returned = new TreeSet()
def max = Math.sqrt(number).longValue()+1L
def range = max..1L
for(factor in range) {
if(!returned.contains(factor)) {
if(number%factor==0) {
returned.add(factor)
returned.add((number/factor).longValue())
/* // Now check already existing dividers. if factor is in it, use this knowledge wisely