Skip to content

Instantly share code, notes, and snippets.

@nuclearace
nuclearace / main.swift
Last active March 20, 2020 16:47
birthday problem
func equalBirthdays(nSharers: Int, groupSize: Int, nRepetitions: Int) -> Double {
var eq = 0
for _ in 0..<nRepetitions {
var group = Array(repeating: 0, count: 365)
for _ in 0..<groupSize {
group[Int.random(in: 0..<365)] += 1
}
@nuclearace
nuclearace / main.swift
Last active February 21, 2020 15:11
Simple DB
import Foundation
struct DatabaseEntry: Codable {
let desc: String
let category: String
let added: Date
let data: [String: String]
}
extension DatabaseEntry: CustomStringConvertible {
@nuclearace
nuclearace / main.swift
Created January 8, 2020 16:47
superpermutations
let max = 15
var len = 0
var counts = [Int](repeating: 0, count: max)
var pos = 0
func factSum(n: Int) -> Int {
var s = 0
var x = 0
var f = 1
@nuclearace
nuclearace / chem.swift
Created January 6, 2020 15:09
molar mass
public struct Chem {
private struct Molecule {
var formula: String
var parts: [Molecule]
var quantity = 1
var molarMass: Double {
switch parts.count {
case 0:
return Chem.atomicWeights[formula]! * Double(quantity)
@nuclearace
nuclearace / main.swift
Created January 1, 2020 18:06
Cheryl's birthday
struct MonthDay: CustomStringConvertible {
static let months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
]
var month: Int
var day: Int
var description: String { "\(MonthDay.months[month - 1]) \(day)" }
extension BinaryInteger {
@inlinable
public func power(_ n: Self) -> Self {
return stride(from: 0, to: n, by: 1).lazy.map({_ in self }).reduce(1, *)
}
}
func continuedFraction<T: Sequence, V: Sequence>(
_ seq1: T,
_ seq2: V,
@nuclearace
nuclearace / main.swift
Created December 20, 2019 12:21
moving average
struct SimpleMovingAverage {
var period: Int
var numbers = [Double]()
mutating func addNumber(_ n: Double) -> Double {
numbers.append(n)
if numbers.count > period {
numbers.removeFirst()
}
@nuclearace
nuclearace / main.swift
Created December 6, 2019 15:37
Dijkstra's algorithm (Requiures Swift Alg Club's Heap)
typealias WeightedEdge = (Int, Int, Int)
struct Grid<T> {
var nodes: [Node<T>]
mutating func addNode(data: T) -> Int {
nodes.append(Node(data: data, edges: []))
return nodes.count - 1
}
@nuclearace
nuclearace / main.swift
Created December 6, 2019 15:37
Dijkstra's algorithm (Requiures Swift Alg Club's Heap)
typealias WeightedEdge = (Int, Int, Int)
struct Grid<T> {
var nodes: [Node<T>]
mutating func addNode(data: T) -> Int {
nodes.append(Node(data: data, edges: []))
return nodes.count - 1
}
@nuclearace
nuclearace / main.swift
Last active November 27, 2019 18:25
Lychrel numbers
// Output
// $ time ./.build/x86_64-apple-macosx/release/Runner
// Found 249 Lychrel numbers between 1...10_000 when limited to 500 iterations
// Number of Lychrel seeds found: 5
// Lychrel seeds found: [196, 879, 1997, 7059, 9999]
// Number of related Lychrel nums found: 244
// Lychrel palindromes found: [4994, 8778, 9999]
// real 0m0.300s
// user 0m0.289s