Skip to content

Instantly share code, notes, and snippets.

View thiagoa's full-sized avatar

Thiago Araújo Silva thiagoa

  • thoughtbot
  • Natal / RN - Brazil
View GitHub Profile
const MAX = 1000000;
const cache = new Array(MAX);
function nextFor(n) {
if (n == 1) {
return 1;
}
else if (n <= MAX && cache[n - 1] !== undefined) {
return cache[n - 1];
}
ZERO = Int64.new(0)
ONE = Int64.new(1)
TWO = Int64.new(2)
THREE = Int64.new(3)
ONE_MILLION = Int64.new(1_000_000)
class Collatz
def initialize(min : Int64, max : Int64)
@min = min
@max = max
defmodule Collatz do
def call(n, cache), do: call(n, 0, cache)
def call(1, length, _), do: length + 1
def call(n, length, cache), do: get(n, length, cache, cache[n])
defp get(n, length, cache, nil), do: call(next(n), length, cache) + 1
defp get(_, _, _, value), do: value
defp next(n) when rem(n, 2) == 0, do: div(n, 2)
defp next(n) when rem(n, 2) == 1, do: n * 3 + 1
@thiagoa
thiagoa / elixir_bubble_sort.ex
Last active June 9, 2017 01:55
Naive and Effective Bubble Sort in Elixir
defmodule NaiveBubbleSort do
def call([]), do: []
def call(list), do: call(list, 0, length(list) - 1, true)
def call(list, idx, last_idx, false) when last_idx == idx, do: list |> call
def call(list, idx, last_idx, true) when last_idx == idx, do: list
def call(list, idx, last_idx, done) do
{a, b} = values_to_compare(list, idx)
{list, done} = process(list, idx, a, b, done)
require 'minitest/autorun'
class Item
include Comparable
def initialize(item, priority:)
@item = item
@priority = priority
end
defmodule FunctionLogger do
defmacro __using__(_) do
quote do
import Kernel, except: [def: 2]
import FunctionLogger
end
end
defmacro def(fun_def, do: block) do
quote do
class HStruct
def self.new(*members, defaults: {}, &block)
klass = Class.new do
@members = members
@defaults = defaults
class << self
attr_reader :members, :defaults
end
sdfsdfsdfs
sfsd
sfds
defmodule PrimeCalculator do
@moduledoc """
Returns all prime numbers within a range
"""
import Enum, only: [sort: 1, to_list: 1, filter: 2]
import Map, only: [keys: 1, put: 3]
import Float, only: [ceil: 1]
import :math, only: [sqrt: 1]