Skip to content

Instantly share code, notes, and snippets.

class PerfectNumber
def self.classify(num)
raise RuntimeError if num < 1
aliquot_sum = (1..(num / 2)).select {|int| num % int == 0}.reduce(:+)
if aliquot_sum > num
"abundant"
elsif aliquot_sum < num

Perfect Numbers

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for natural numbers.

The Greek mathematican Nicomachus devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum. The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

  • Perfect: aliquot sum = number
    • 6 is a perfect number because (1 + 2 + 3) = 6
    • 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
  • Abundant: aliquot sum > number
class Main {
public static void main(String[] args) {
for(int num = 1; num <= 100; num++)
System.out.println(convertToRoman(num));
}
public static String convertToRoman(int num) {
String number = "";
int M, D, C, L, X, V, I;
M = D = C = L = X = V = I = 0;

Roman Numerals

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date

require "benchmark"
class Prime
def self.nth(num)
raise ArgumentError if num == 0 || num.class != Fixnum
count = 0
check = 1
while count != num
class Prime
def nth(num)
end
end
class PhoneNumber
def initialize(raw_string)
@clean_string = raw_string.gsub(/[\(\)\-\s\.]/, "")
@clean_string[0] = "" if @clean_string[0] == "1" && @clean_string.length == 11
end
def number
valid_clean_string? ? @clean_string : '0000000000'
end

Phone Number

Clean up user-entered phone numbers so that they can be sent SMS messages.

The rules are as follows:

  • If the phone number is less than 10 digits assume that it is bad number
  • If the phone number is 10 digits assume that it is good
  • If the phone number is 11 digits and the first number is 1, trim the 1
@dmill
dmill / dmill_code_sample.rb
Created January 24, 2014 03:54
Doug Mill Code Sample
def return_all_file_names_and_paths(username, repo, path = nil, files = {})
uri_string = "https://api.github.com/repos/#{username}/#{repo}/contents/#{path}"
root_directory = make_api_call(uri_string)
root_directory.each do |item|
if item["type"] == "file"
files[item["name"]] = item["path"]
elsif item["type"] == "dir"
return_all_file_names_and_paths(username, repo, item["path"], files )
end
@dmill
dmill / lyft.js
Last active January 3, 2016 09:49
//point 1 and point 2 are arrays representing tuples of x,y coordinates.
function distance(point1, point2) {
return Math.sqrt(Math.pow((point1[0] - point2[0]), 2) + Math.pow((point1[1] - point2[1]), 2))
}
// a and b are driver 1's starting and ending locations, in the form of arrays representing tuples
//of x,y coordinates. c and d similarly correspond to driver 2. Returns the shorter overall trip.
function shortestTrip (a,b,c,d) {
var driver1Distance = distance(a,c) + distance(c,d) + distance(c,b)
var driver2Distance = distance(c,a) + distance(a,b) + distance(b,d)