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
(define (abs x) | |
(cond ((= 0 x) 0) | |
((< 0 x) x) | |
((> 0 x)(- 0 x)))) | |
(define abs | |
(lambda (x) | |
(cond ((= 0 x) 0) | |
((< 0 x) x) | |
((> 0 x)(- 0 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
// Find the sum of all the multiples of 3 or 5 below 1000. | |
// | |
// | |
// ruby version: (inspired by Lfborjas' Ruby Fuckery) | |
//multi = ->(x) { (x % 3).zero? or (x % 5).zero? } | |
//puts (1...1000).select{ |x| multi[x] }.inject(:+).to_s | |
//233168 | |
function arrayFromRange(lo, hi){ |
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
class Array | |
def insert_every!(skip, str) | |
orig_ary_length = self.length | |
new_ary_length = orig_ary_length + ((orig_ary_length-1) / skip) | |
i = skip | |
while(i<new_ary_length) do self.insert(i,str); i+=(skip+1) end | |
self | |
end | |
end |
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
=begin | |
The sum of the squares of the first ten natural numbers is, | |
1**2 + 2**2 + ... + 102 = 385 | |
The square of the sum of the first ten natural numbers is, | |
(1 + 2 + ... + 10)**2 = 3025 | |
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640. | |
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the 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
# Euclid's formula yields Pythagorean triples for integers m and n with m < n: | |
# a = m**2 - n**2 ; b = 2*m*n ; c = m**2 + n**2 | |
x = 1000 | |
def euclids upto | |
result = [] | |
(2..upto).each do |m| # Start at 2 as 1 results nothing anyway | |
(1...m).each do |n| # Euclid's formula only works for m > n | |
result << [m**2 - n**2, 2*m*n, m**2 + n**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
# goal: get a number like 25164150 | |
# and transform it to: 25,164,150 | |
# =begin | |
# 1) number.to_s.split("").length | |
# 2) start from end, count multiples of 3 | |
# 3) insert comma | |
# 4) turn array back into a string | |
# .join(",") | |
# x.unshift(",")unless (x % 3 != 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 modulo_3?(number) | |
# (number % 3).zero? | |
number.modulo(3).zero? # i guess this is more idiomatic Ruby, though I like the previous better | |
end | |
def commify(number) | |
reversed = number.to_s.reverse | |
number_size = number.to_s.size | |
result = "" |
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
# Find the greatest product of five consecutive digits in the 1000-digit number. | |
madness=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315 |
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
class Celery | |
def initialize(grid) | |
@grid = grid | |
@height = grid.length | |
@width = grid[0].length | |
end | |
def self.mini | |
[[ 0, 1, 2, 3, 4], | |
[10,11,12,13,14], | |
[20,21,22,23,24], |
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
# proccc | |
def one proccc, x | |
proccc.(x) | |
end | |
def two proccc, x | |
proccc.(proccc.(x)) | |
end |