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
(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 | |
} |
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
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) { |
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
BigInteger.metaClass.lcm = { number, value = longValue() -> | |
number * value / gcd(number) as BigInteger | |
} | |
(1..20).inject 1, { BigInteger result, it -> result.lcm it } |
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
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() |
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
def reallyBigNumbers = | |
'''37107287533902102798797998220837590246510135740250 | |
46376937677490009712648124896970078050417018260538 | |
74324986199524741059474233309513058123726617309629 | |
91942213363574161572522430563301811072406154908250 | |
23067588207539346171171980310421047513778063246676 | |
89261670696623633820136378418383684178734361726757 | |
28112879812849979408065481931592621691275889832738 | |
44274228917432520321923589422876796487670272189318 | |
47451445736001306439091167216856844588711603153276 |
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
String sumOfPowers = (1..1000).inject(0) { sum, it -> | |
sum += it ** it | |
} | |
sumOfPowers[-10..-1] |
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
// 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 | |
} | |
} | |
} |
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
def candidate = 3 | |
def listOfPrimes = [2] | |
while (listOfPrimes.size() < 10001) { | |
max = Math.sqrt(candidate) | |
for (prime in listOfPrimes) { | |
if (prime > max) { | |
listOfPrimes << candidate | |
break | |
} |
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
def appendIsSo (adjective, names) { | |
names?.to*.plus " is so ${adjective}." | |
} | |
appendIsSo 'awesome', [to: ['Aby', 'Brooke', 'Claire']] | |
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
(1..100).inject 0, { total, base -> | |
(1..100).each { power -> | |
String number = base ** power | |
if (power == number.size()) { total += 1 } | |
} | |
total | |
} |