This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 largestPrimeFactorOf = { BigInteger it, divisor = 1, factors = [] -> | |
| while (it > 1) { | |
| divisor += 1 | |
| while (it % divisor == 0) { | |
| factors << divisor | |
| it /= divisor | |
| } | |
| } | |
| factors.last() | |
| } |
This file contains hidden or 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 list = [0, 1] | |
| while (list.last() < 4000000) { | |
| list << list[-1] + list[-2] | |
| } | |
| list.findAll { it % 2 == 0 }.sum() |
This file contains hidden or 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).sum() ** 2 - (1..100).collect { it ** 2 }.sum() |
This file contains hidden or 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..<1000).findAll { it % 3 == 0 || it % 5 == 0 }.sum() |
This file contains hidden or 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
| 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(); |
This file contains hidden or 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
| 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); | |
| } |