Skip to content

Instantly share code, notes, and snippets.

View syntacticsugar's full-sized avatar
🎯
Focusing

RomyRomy syntacticsugar

🎯
Focusing
View GitHub Profile
(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)))))
@syntacticsugar
syntacticsugar / gist:5059926
Created February 28, 2013 20:40
Euler #1, in bloated, x-rated, nasty, perverse, lecherous, spaghetti Javascript ! (evil cackle)
// 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){
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
@syntacticsugar
syntacticsugar / 6th_euler.rb
Created March 7, 2013 20:42
Euler #6, coded on the subway in Washington Heights. "Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."
=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.
# 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]
@syntacticsugar
syntacticsugar / commify_numbers.rb
Created March 20, 2013 04:09
# goal: get a number like 25164150 # and transform it to: 25,164,150
# 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)
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 = ""
@syntacticsugar
syntacticsugar / euler8.rb
Created March 27, 2013 00:27
Find the greatest product of five consecutive digits in the 1000-digit number.
# Find the greatest product of five consecutive digits in the 1000-digit number.
madness=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315
@syntacticsugar
syntacticsugar / euler11.rb
Created March 28, 2013 19:19
What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20x20 grid?
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],
@syntacticsugar
syntacticsugar / church_encoding.rb
Created April 8, 2013 17:58
In mathematics, Church encoding is a means of embedding data and operators into the lambda calculus, the most familiar form being the Church numerals, a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded data in the lambda calculus this way. Terms that are usually considered prim…
# proccc
def one proccc, x
proccc.(x)
end
def two proccc, x
proccc.(proccc.(x))
end