Skip to content

Instantly share code, notes, and snippets.

// https://twitter.com/koher/status/1627284858493075463
for n in 2 ... 1000000 {
var a = n * n - 1
while a.isMultiple(of: 2) {
a /= 2
}
while a.isMultiple(of: 5) {
a /= 5
}
guard a == 1 else { continue }
import Foundation
actor UUIDSerialNumberGenerator {
static let shared: UUIDSerialNumberGenerator = .init()
private var nextSerialNumber: Int = 0
private var uuidToSerialNumber: [UUID: Int] = [:]
private init() {}

標準入力で1以上の整数が一つ与えられます。このとき、1から与えられた整数までの合計を計算して出力するプログラムを、Swiftで書いて下さい。


次のようなSwiftプログラムを書くことで、1から与えられた整数までの合計を計算して出力できます。

import Foundation

// 入力
import CoreGraphics
let vectors: [CGVector] = [CGVector(dx: 2, dy: 1), CGVector(dx: -3, dy: 0)]
// ∞ノルムの平均の計算
// extension なし
vectors.lazy
.map { max($0.dx.magnitude, $0.dy.magnitude) }
.reduce(0, +) / CGFloat(vectors.count)
@koher
koher / JSON.swift
Created October 23, 2021 00:32
Codable JSON type in Swift
enum JSON {
case number(Double)
case boolean(Bool)
case string(String)
case array([JSON])
case object([String: JSON])
case null
}
extension JSON: Codable {
// https://algo-method.com/tasks/302
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) {
let values = readLine()!.split(separator: " ").map { Int(String($0))! }
precondition(values.count == 3)
return (values[0], values[1], values[2])
}
var (n, x, y) = readInt3()
for _ in 2 ..< n {
let aa = (1...100).shuffled().dropFirst(50).sorted()
print(aa.count, 100)
print(aa.map(\.description).joined(separator: " "))
let aaSet = Set(aa)
var count = 0
var ans: [Int] = []
for i in 1 ... 1000 {
if aaSet.contains(i) { continue }
let pp = readLine()!.split(separator: " ")
let e = pp[0]
let y = Int(pp[1])!
let m = Int(pp[2])!
let d = Int(pp[3])!
let dd = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
struct YMD: Comparable {
let y: Int
func binarySearch<Values>(_ value: Values.Element, in values: Values) -> Int? where Values: Collection, Values.Element: Comparable, Values.Index == Int {
var values: Values.SubSequence = values[...]
while !values.isEmpty {
let midIndex = values.startIndex + (values.endIndex - values.startIndex) / 2
let midValue = values[midIndex]
if value < midValue {
values = values[..<midIndex]
} else if value > midValue {
values = values[(midIndex + 1)...]
} else {
@koher
koher / ContentView.swift
Created April 3, 2021 16:10
An example how to use onReceive with @published
import SwiftUI
struct ContentView: View {
@StateObject private var state: ContentViewState = .init()
var body: some View {
VStack {
Text(state.count.description)
Button("Count Up") { state.countUp() }
Button("Reset") { state.reset() }
}