Skip to content

Instantly share code, notes, and snippets.

@jaredcacurak
jaredcacurak / euler30.groovy
Created February 24, 2011 02:18
A Groovy solution for Project Euler - Problem 30
Integer.metaClass.toDigits = {
value.toString().toList().collect { it as Integer }
}
Integer.metaClass.digitsToThePowerOf = { power ->
toDigits().collect { it ** power }
}
(1000..200000).inject 0, { sum, number ->
sumOfDigitsToTheFifthPower = number.digitsToThePowerOf(5).sum()
@jaredcacurak
jaredcacurak / euler5.groovy
Created February 24, 2011 02:19
A Groovy solution for Project Euler - Problem 5
BigInteger.metaClass.lcm = { number, value = longValue() ->
number * value / gcd(number) as BigInteger
}
(1..20).inject 1, { BigInteger result, it -> result.lcm it }
@jaredcacurak
jaredcacurak / passByWhat.java
Created February 24, 2011 02:21
Pass by what?
public class Main {
private static String boo;
public static void main(String[] args) {
boo = "value";
changeTheValueOf(boo);
System.out.println("passed by " + boo);
}
private static void changeTheValueOf(String value) {
@jaredcacurak
jaredcacurak / euler4.groovy
Created February 24, 2011 02:29
A Groovy solution for Project Euler - Problem 4
(999..100).inject 0, { max, a ->
(a..100).each { b ->
String product = a * b
if (product.reverse() == product)
max = [max, product as Integer].max()
}
max
}
@jaredcacurak
jaredcacurak / euler3.groovy
Created February 24, 2011 02:31
A Groovy solution for Project Euler - Problem 3
def largestPrimeFactorOf = { BigInteger it, divisor = 1, factors = [] ->
while (it > 1) {
divisor += 1
while (it % divisor == 0) {
factors << divisor
it /= divisor
}
}
factors.last()
}
@jaredcacurak
jaredcacurak / euler2.groovy
Created February 24, 2011 02:32
A Groovy solution for Project Euler - Problem 2
def list = [0, 1]
while (list.last() < 4000000) {
list << list[-1] + list[-2]
}
list.findAll { it % 2 == 0 }.sum()
@jaredcacurak
jaredcacurak / euler6.groovy
Created February 24, 2011 02:33
A Groovy solution for Project Euler - Problem 6
(1..100).sum() ** 2 - (1..100).collect { it ** 2 }.sum()
@jaredcacurak
jaredcacurak / euler1.groovy
Created February 24, 2011 02:34
A Groovy solution for Project Euler - Problem 1
(1..<1000).findAll { it % 3 == 0 || it % 5 == 0 }.sum()
@jaredcacurak
jaredcacurak / NumberMuncher.java
Created February 24, 2011 02:37
A Groovy + Java solution for Project Euler - Problem 30
class NumberMuncher {
private final Integer value;
NumberMuncher(Integer value) {
this.value = value;
}
List<Integer> digitsToThePowerOf(int power) {
List<Integer> listOfPowers = new ArrayList<Integer>();
List<Integer> digits = toDigits();
function LuhnyBin(creditCard, minLength, maxLength, maskWithChar) {
if (this instanceof LuhnyBin) {
this.creditCard = creditCard;
this.minLength = minLength || 14;
this.maxLength = maxLength || 16;
this.maskWithChar = maskWithChar || 'X';
this.output = this.process(creditCard);
} else {
return new LuhnyBin(creditCard, minLength, maxLength, maskWithChar);
}