Skip to content

Instantly share code, notes, and snippets.

@proxpero
proxpero / project_euler_90.swift
Last active October 9, 2015 21:26
Project Euler problem 90 in Swift
//: Cube digit pairs
//: [Problem 90](https://projecteuler.net/problem=90)
//: Xcode 7.0, Swift 2.0
/*:
Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers.
For example, the square number 64 could be formed:
![Two cubes showing 64](https://projecteuler.net/project/images/p090.gif)
In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81.
@proxpero
proxpero / project_euler_44.swift
Last active October 11, 2015 01:37
Project Euler problem 44: Pentagonal Numbers (Xcode 7.0, Swift 2.0)
//: [Pentagonal Numbers](https://projecteuler.net/problem=44)
/*:
Pentagonal numbers are generated by the formula, P[n]=n[3n−1]/2. The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
It can be seen that P[4] + P[7] = 22 + 70 = 92 = P[8]. However, their difference, 70 − 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, P[j] and P[k], for which their sum and difference are pentagonal and D = |P[k] − P[j]| is minimised; what is the value of D?
*/
@proxpero
proxpero / project_euler_43.swift
Created October 13, 2015 18:08
Project Euler problem 43 in Swift
//: [Sub-string Divisibility](https://projecteuler.net/problem=43)
/*:
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
Let d[1] be the 1st digit, d[2] be the 2nd digit, and so on. In this way, we note the following:
* d[2]d[3]d[4] = 406 is divisible by 2
* d[3]d[4]d[5] = 063 is divisible by 3
* d[4]d[5]d[6] = 635 is divisible by 5
* d[5]d[6]d[7] = 357 is divisible by 7
@proxpero
proxpero / NSColor+RGB
Created January 18, 2016 13:32
An simple extension to NSColor to create a color from a six character RGB string. Written in Swift.
// Swift 2.2
extension NSColor {
convenience init(rgb: String) {
guard rgb.characters.count == 6 else { fatalError("Invalid rgb value: \(rgb)") }
let scanner = NSScanner(string: rgb)
var hexValue: UInt32 = 0
@proxpero
proxpero / SelectionSort.swift
Last active January 27, 2016 18:35
Selection sort in Swift implemented as an extension on `MutableCollectionType`
// Swift 2.1
extension MutableCollectionType where Self.Generator.Element : Comparable {
/// Return a new sorted collection of the elements in `source`
/// using [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort).
///
/// Complexity O(n^2).
@warn_unused_result(mutable_variant="selectionSortInPlace")
public func selectionSort() -> Self {
@proxpero
proxpero / InsertionSort.swift
Last active January 27, 2016 18:38
Insertion Sort in Swift implemented as an extension on `MutableCollectionType`
// Swift 2.1
extension MutableCollectionType where Self.Generator.Element : Comparable {
/// Return a new sorted collection of the elements in `source`
/// using [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort).
///
/// Complexity O(n^2).
@warn_unused_result(mutable_variant="insertionSortInPlace")
public func insertionSort() -> Self {
@proxpero
proxpero / ShellSort.swift
Created January 27, 2016 21:19
Sort a collection using Shell's method. Implemented in Swift as an extension on `MutableCollectionType`.
// Swift 2.1
extension MutableCollectionType where Self.Generator.Element : Comparable, Self.Index == Int {
/// Return a new sorted collection of the elements in `source`
/// using [Shell Sort](https://en.wikipedia.org/wiki/Shellsort).
///
/// Complexity O(n^(3/2)).
@warn_unused_result(mutable_variant="shellSortInPlace")
@proxpero
proxpero / MergeSort.swift
Last active February 8, 2016 17:29
A simple implementation of Merge Sort in Swift as an extension on Array
// Swift 2.1
extension Array where Element : Comparable {
private func merge(var left: Array<Element>, var _ right: Array<Element>) -> Array<Element> {
var result: Array<Element> = []
while !left.isEmpty && !right.isEmpty {
@proxpero
proxpero / md5.swift
Last active February 16, 2017 16:48
Small, zero-dependency extensions on `String` and Collections of `UInt8` that return the md5 hash of self.
// As described in: http://proxpero.com/2017/02/14/an-implementation-of-md5/
extension Collection where Iterator.Element == UInt8 {
/// Returns the md5 hash of self.
public var md5: String {
return self.md5Digest.lazy.reduce("") {
var s = String($1, radix: 16)
if s.characters.count == 1 {
s = "0" + s

Keybase proof

I hereby claim:

  • I am proxpero on github.
  • I am proxpero (https://keybase.io/proxpero) on keybase.
  • I have a public key whose fingerprint is ABAF 07DE 0D7E EA58 7C60 1131 CB36 31B5 E8CA 11C7

To claim this, I am signing this object: