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
final def MAX = 1000 | |
def sum = 0 | |
(3..MAX-1).each { | |
if (it % 3 == 0 || it % 5 == 0) { | |
sum += it | |
} | |
} | |
println "sum => $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
(num += num if num % 3 == 0 || num % 5 == 0 for num in [1..1000]) | |
alert num |
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 MAX = 4000000 | |
def fibonacci = [1,2] | |
def num = 3 | |
while(num < MAX) { | |
fibonacci << num | |
num = fibonacci.get(fibonacci.size() - 2) + fibonacci.get(fibonacci.size() - 1) | |
} | |
def evens = fibonacci.grep { it % 2 == 0 } |
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 primes = [2, 3, 5, 7, 11, 13, 17] | |
def MAX = 600851475143 | |
def factor = 0 | |
def i = primes.get(primes.size() - 1) + 1 | |
while (MAX > 1) { | |
if(!primes.any { i % it == 0 }) { | |
primes << i | |
if (MAX % i == 0) { | |
factor = i | |
MAX = (long)(MAX/i) |
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 x = 999 | |
def y = 999 | |
def found = false | |
def p | |
while (!found) { | |
p = String.valueOf(x * y) | |
def same = true | |
for (int i = 0; i < p.length() / 2; i++) { | |
if (p.charAt(i) != p.charAt(p.length() - i - 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
def MIN = 2, MAX = 20 | |
def primes = [2, 3, 5, 7, 11, 13, 17, 19] | |
def factors = [] | |
while (MIN <= MAX) { | |
if (MIN in primes) { | |
factors << MIN | |
} else { | |
// find MIN factors | |
def fn = [] | |
def x = MIN |
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 s1 = 0l, s2 = 0l | |
for (int i = 1; i <= 100; i++) { | |
s1 += (long)Math.pow(i, 2) | |
s2 += i | |
} | |
def x = (long)(Math.pow(s2, 2) - s1) | |
println x |
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 primes = [2, 3, 5, 7, 11, 13, 17] | |
def MAX = 10001 | |
def i = primes.get(primes.size() - 1) + 1 | |
while (primes.size() < MAX) { | |
if(!primes.any { i % it == 0 }) { | |
primes << i | |
} | |
i++ | |
} |
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 t = 1000 | |
def max_a = 332 | |
def s = [] | |
def max_b, min_c | |
for (int a = max_a; a > 0; a--) { | |
int c = min_c = (int)((t - a) / 2 + 1) | |
int b = max_b = t - a - min_c | |
while (c > b && b > a) { | |
def a2 = Math.pow(a, 2) |
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 primes = [2, 3, 5, 7, 11, 13, 17, 19] | |
def MAX = 2000000 | |
double sum = primes.sum() | |
def i = primes.get(primes.size() - 1) + 2 | |
while (MAX > i) { | |
def found = true | |
for (int x = 0; x < primes.size() && primes.get(x)*primes.get(x) <= i; x++) { | |
if( i % primes.get(x) == 0) { | |
found = false | |
break |
OlderNewer