Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / euler13.groovy
Created February 24, 2011 02:15
A Groovy solution for Project Euler - Problem 13
def reallyBigNumbers =
'''37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
@jaredcacurak
jaredcacurak / euler48.groovy
Created February 24, 2011 02:13
A Groovy solution for Project Euler - Problem 48
​String sumOfPowers = (1..1000).inject(0) { sum, it ->
sum += it ** it
}
sumOfPowers[-10..-1]
@jaredcacurak
jaredcacurak / euler9.groovy
Created February 24, 2011 02:11
A Groovy solution for Project Euler - Problem 9
// brute force
100.upto 300, { a ->
a.upto 400, { b ->
b.upto 500, { c ->
if (a + b + c == 1000 && a * a + b * b == c * c)
println "$a * $b * $c = " + a * b * c
}
}
}
@jaredcacurak
jaredcacurak / euler7.groovy
Created February 24, 2011 02:06
A Groovy solution for Project Euler - Problem 7
def candidate = 3
def listOfPrimes = [2]
while (listOfPrimes.size() < 10001) {
max = Math.sqrt(candidate)
for (prime in listOfPrimes) {
if (prime > max) {
listOfPrimes << candidate
break
}
@jaredcacurak
jaredcacurak / NameIsSoAdjective.groovy
Created January 28, 2011 22:00
Create a function that accepts a collection of names and an adjective then returns a new collection of values in the form of, "NAME is so ADJECTIVE."
def appendIsSo (adjective, names) {
names?.to*.plus " is so ${adjective}."
}
appendIsSo 'awesome', [to: ['Aby', 'Brooke', 'Claire']]
@jaredcacurak
jaredcacurak / euler63.groovy
Created January 25, 2011 22:37
A Groovy solution for Project Euler - Problem 63
(1..100).inject 0, { total, base ->
(1..100).each { power ->
String number = base ** power
if (power == number.size()) { total += 1 }
}
total
}