Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,24 +1,7 @@
|
|||
function isperfect{T<:Integer}(n::T)
|
||||
!isprime(n) || return false
|
||||
tal = 1//n
|
||||
hi = isqrt(n)
|
||||
if hi^2 == n
|
||||
tal += 1//hi
|
||||
hi -= 1
|
||||
end
|
||||
for i in 2:hi
|
||||
(d, r) = divrem(n, i)
|
||||
if r == 0
|
||||
tal += (1//i + 1//d)
|
||||
end
|
||||
end
|
||||
return tal == 1//1
|
||||
end
|
||||
using Primes
|
||||
divisors(n) = foldl((a, (p, e)) -> vcat((a * [p^i for i in 0:e]')...), factor(n), init=[1])
|
||||
|
||||
lo = 2
|
||||
hi = 2^19
|
||||
println("Searching for perfect numbers from ", lo, " to ", hi, ".")
|
||||
for i in 2:2^19
|
||||
isperfect(i) || continue
|
||||
println(@sprintf("%8d", i))
|
||||
end
|
||||
isperfect(n) = sum(1 // d for d in divisors(n)) == 2
|
||||
|
||||
lo, hi = 2, 2^19
|
||||
println("Perfect numbers between ", lo, " and ", hi, ": ", collect(filter(isperfect, lo:hi)))
|
||||
|
|
|
|||
156
Task/Arithmetic-Rational/Swift/arithmetic-rational.swift
Normal file
156
Task/Arithmetic-Rational/Swift/arithmetic-rational.swift
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
import Foundation
|
||||
|
||||
extension BinaryInteger {
|
||||
@inlinable
|
||||
public func gcd(with other: Self) -> Self {
|
||||
var gcd = self
|
||||
var b = other
|
||||
|
||||
while b != 0 {
|
||||
(gcd, b) = (b, gcd % b)
|
||||
}
|
||||
|
||||
return gcd
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func lcm(with other: Self) -> Self {
|
||||
let g = gcd(with: other)
|
||||
|
||||
return self / g * other
|
||||
}
|
||||
}
|
||||
|
||||
public struct Frac<NumType: BinaryInteger & SignedNumeric>: Equatable {
|
||||
@usableFromInline
|
||||
var _num: NumType
|
||||
|
||||
@usableFromInline
|
||||
var _dom: NumType
|
||||
|
||||
@usableFromInline
|
||||
init(_num: NumType, _dom: NumType) {
|
||||
self._num = _num
|
||||
self._dom = _dom
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init(numerator: NumType, denominator: NumType) {
|
||||
let divisor = numerator.gcd(with: denominator)
|
||||
|
||||
self._num = numerator / divisor
|
||||
self._dom = denominator / divisor
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func + (lhs: Frac, rhs: Frac) -> Frac {
|
||||
let multiplier = lhs._dom.lcm(with: rhs.denominator)
|
||||
|
||||
return Frac(
|
||||
numerator: lhs._num * multiplier / lhs._dom + rhs._num * multiplier / rhs._dom,
|
||||
denominator: multiplier
|
||||
)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func += (lhs: inout Frac, rhs: Frac) {
|
||||
lhs = lhs + rhs
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func - (lhs: Frac, rhs: Frac) -> Frac {
|
||||
return lhs + -rhs
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func -= (lhs: inout Frac, rhs: Frac) {
|
||||
lhs = lhs + -rhs
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func * (lhs: Frac, rhs: Frac) -> Frac {
|
||||
return Frac(numerator: lhs._num * rhs._num, denominator: lhs._dom * rhs._dom)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func *= (lhs: inout Frac, rhs: Frac) {
|
||||
lhs = lhs * rhs
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func / (lhs: Frac, rhs: Frac) -> Frac {
|
||||
return lhs * Frac(_num: rhs._dom, _dom: rhs._num)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func /= (lhs: inout Frac, rhs: Frac) {
|
||||
lhs = lhs / rhs
|
||||
}
|
||||
|
||||
@inlinable
|
||||
prefix static func - (rhs: Frac) -> Frac {
|
||||
return Frac(_num: -rhs._num, _dom: rhs._dom)
|
||||
}
|
||||
}
|
||||
|
||||
extension Frac {
|
||||
@inlinable
|
||||
public var numerator: NumType {
|
||||
get { _num }
|
||||
set {
|
||||
let divisor = newValue.gcd(with: denominator)
|
||||
|
||||
_num = newValue / divisor
|
||||
_dom = denominator / divisor
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var denominator: NumType {
|
||||
get { _dom }
|
||||
set {
|
||||
let divisor = newValue.gcd(with: numerator)
|
||||
|
||||
_num = numerator / divisor
|
||||
_dom = newValue / divisor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Frac: CustomStringConvertible {
|
||||
public var description: String {
|
||||
let neg = numerator < 0 || denominator < 0
|
||||
|
||||
return "Frac(\(neg ? "-" : "")\(abs(numerator)) / \(abs(denominator)))"
|
||||
}
|
||||
}
|
||||
|
||||
extension Frac: Comparable {
|
||||
@inlinable
|
||||
public static func <(lhs: Frac, rhs: Frac) -> Bool {
|
||||
return lhs._num * rhs._dom < lhs._dom * rhs._num
|
||||
}
|
||||
}
|
||||
|
||||
extension Frac: ExpressibleByIntegerLiteral {
|
||||
public init(integerLiteral value: Int) {
|
||||
self._num = NumType(value)
|
||||
self._dom = 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for candidate in 2..<1<<19 {
|
||||
var sum = Frac(numerator: 1, denominator: candidate)
|
||||
|
||||
let m = Int(ceil(Double(candidate).squareRoot()))
|
||||
|
||||
for factor in 2..<m where candidate % factor == 0 {
|
||||
sum += Frac(numerator: 1, denominator: factor)
|
||||
sum += Frac(numerator: 1, denominator: candidate / factor)
|
||||
}
|
||||
|
||||
if sum == 1 {
|
||||
print("\(candidate) is perfect")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue