Skip to content

Instantly share code, notes, and snippets.

// 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 {
@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 {
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)

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


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

import Foundation

// 入力
import Foundation
actor UUIDSerialNumberGenerator {
static let shared: UUIDSerialNumberGenerator = .init()
private var nextSerialNumber: Int = 0
private var uuidToSerialNumber: [UUID: Int] = [:]
private init() {}
// 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 }