This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct DatabaseEntry: Codable { | |
let desc: String | |
let category: String | |
let added: Date | |
let data: [String: String] | |
} | |
extension DatabaseEntry: CustomStringConvertible { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SimpleMovingAverage { | |
var period: Int | |
var numbers = [Double]() | |
mutating func addNumber(_ n: Double) -> Double { | |
numbers.append(n) | |
if numbers.count > period { | |
numbers.removeFirst() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
NewerOlder