Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Mertens-function/Swift/mertens-function.swift
Normal file
41
Task/Mertens-function/Swift/mertens-function.swift
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import Foundation
|
||||
|
||||
func mertensNumbers(max: Int) -> [Int] {
|
||||
var mertens = Array(repeating: 1, count: max + 1)
|
||||
for n in 2...max {
|
||||
for k in 2...n {
|
||||
mertens[n] -= mertens[n / k]
|
||||
}
|
||||
}
|
||||
return mertens
|
||||
}
|
||||
|
||||
let max = 1000
|
||||
let mertens = mertensNumbers(max: max)
|
||||
|
||||
let count = 200
|
||||
let columns = 20
|
||||
print("First \(count - 1) Mertens numbers:")
|
||||
for i in 0..<count {
|
||||
if i % columns > 0 {
|
||||
print(" ", terminator: "")
|
||||
}
|
||||
print(i == 0 ? " " : String(format: "%2d", mertens[i]), terminator: "")
|
||||
if (i + 1) % columns == 0 {
|
||||
print()
|
||||
}
|
||||
}
|
||||
|
||||
var zero = 0, cross = 0, previous = 0
|
||||
for i in 1...max {
|
||||
let m = mertens[i]
|
||||
if m == 0 {
|
||||
zero += 1
|
||||
if previous != 0 {
|
||||
cross += 1
|
||||
}
|
||||
}
|
||||
previous = m
|
||||
}
|
||||
print("M(n) is zero \(zero) times for 1 <= n <= \(max).")
|
||||
print("M(n) crosses zero \(cross) times for 1 <= n <= \(max).")
|
||||
Loading…
Add table
Add a link
Reference in a new issue