μμ μμ§λ μκ°μ΄κ³Όμ μΈμ°λμ€,, μ§κΈμ 'μκ°μ΄κ³Ό λ κ±°κ°μλ°..'νλ©΄μ μ½λλ₯Ό κ°κ²¨ μ°κ³ μλ€. νμ§λ§,,,,,,,, μ μ΄μ λ¬Έμ νμ΄ μμν λλΆν° μκ°μ΄κ³Όμ λν μκ°μνκ³ μκ³ λ¦¬μ¦μ μ§λκ²....λ§λ€..λ°±λ² λ§λ€. λ΄κ° μλͺ»νκ³ μλκ²μ΄λ€ γ γ
νλμ© ν΄κ²°ν΄λκ°λ©΄μ μκ°μ΄κ³Όμ μμΈμ μ°Ύμ λμλ©΄, μ μ°¨,, μ΅μν΄μ§κ² μ§..?
π λ¬Έμ
https://www.acmicpc.net/problem/6588
π 첫λ²μ§Έ νμ΄ - μμλ μκ°μ΄κ³Ό
private func solution() {
let isprime = primeNumArr()
var primeNum = [Int]()
for i in 1..<isprime.count {
if isprime[i] == true {
primeNum.append(i)
}
}
for _ in 0..<100000 {
var flag = false
let n = Int(readLine()!)!
if n == 0 {
break
} else {
flag = true
for num in primeNum {
if primeNum.contains(n - num) {
print("\(n) = \(num) + \(n-num)")
break
}
}
}
if !flag {
print("Goldbach's conjecture is wrong.")
}
}
}
private func primeNumArr() -> [Bool] {
var isprimenum = Array(repeating: true, count: 1000001)
isprimenum[1] = false
for i in 1..<1000001 {
if isprimenum[i] == false {
continue
}
for j in stride(from: i+i, through: 1000000, by: i) {
isprimenum[j] = false
}
}
return isprimenum
}
solution()
λμ 첫λ²μ§Έ νμ΄λ€. μκ°μ΄κ³Όκ° λ κ±°κ°λ€! νλ©΄μ νμλ€. (.. κ·ΈλΌ.. νμ§λ§μμ΄μΌμ§)
μκ°λ³΅μ‘λλ,,, μ΄μ€ ν¬λ¬Έμ΄ λ€μ΄κ°μλλ§νΌ O(N^2)λ‘ μμλλ€. μ΄λ§μ΄λ§νλ€... μ΄λ λΆλΆμμ ν¨μ¨μ λμΌ μ μμκΉ?
π λλ²μ§Έ νμ΄ - λ§μμ΅λλ€!
μ,, μ¬μ€ λ°κΏμ€κ±΄ λ³λ‘ μλ€. κ·Έλλ νμ€ν for루ν λμκ°λ μκ°μ μ€μλ€. μ΄μ¨λ , μ΄μ€ν¬λ¬Έμ μ¬μ©νκΈ° λλ¬Έμ O(N^2)μ λ§μ§λ§, μν©μ λ°λΌ for 루νκ° λμκ°λ κ²μ μ€μ¬μ€¬λ€κ³ μκ°νλ©΄ λλ€.
λ¨Όμ isprime == true μΈ λΆλΆμ indexλ§ λ½μλ΄λ forλ¬Έμ μμ΄λ€. λλ for λ¬Έμ λλ €μ, μ«μλ₯Ό λΉΌκ³ λν΄μΌν΄μ firstIndexλ₯Ό μ¬μ©ν΄μ μΈλ±μ€κ°μ λ¨Όμ μΆμΆν΄μ€μΌνλ€κ³ μκ°νλ€. μΈλ°μλ forλ¬Έμ΄ ν λ² λ λ€μ΄κ°μ μ΄λ€. κ·Έλ₯, isprimeλ§μ μ¬μ©ν΄μ T/Fλ§ νλ³νλ κ²μ΄ μκ°μ μΌλ‘λ, μ½λμ μΌλ‘λ ν¨μ¬ ν¨μ¨μ μ΄λ€.
// β λλ²μ§Έ μ λ΅μ½λμμλ μ΄ λΆλΆμ μμ νλ€.
for i in 1..<isprime.count {
if isprime[i] == true {
primeNum.append(i)
}
}
λλ²μ§Έλ‘ λ°λ λΆλΆμ μλμ κ°λ€. μ΄μ€ ν¬λ¬Έμ΄ λλ λΆλΆμ 보면 첫λ²μ§Έ νμ΄λ μ΄λ€ μλ€μ νλ³νλ κ°μ, primeNumμ μ λΆ μ΄ν΄λ³΄κ² λλ€. λ§μ λλ²μ§Έ νμ΄λ, 3λΆν° NκΉμ§λ§ 보면μ μ΅λν 보λ for루ν λμκ°λ νμλ₯Ό μ€μ¬μ€¬λ€. κ·Έλ¦¬κ³ if λ¬Έμ μ¬μ©ν΄μ isprimeμμ trueκ°μ κ°λμ§ μ΄ν΄λ§ 쀬λ€.
// β 첫λ²μ§Έ νμ΄
for _ in 0..<100000 {
var flag = false
let n = Int(readLine()!)!
if n == 0 {
break
} else {
flag = true
for num in primeNum {
if primeNum.contains(n - num) {
print("\(n) = \(num) + \(n-num)")
break
}
}
}
if !flag {
print("Goldbach's conjecture is wrong.")
}
}
// β
λλ²μ§Έ νμ΄
for _ in 0..<100000 {
var flag = false
let n = Int(readLine()!)!
if n == 0 {
break
} else {
for i in 3...n {
if isprime[i] {
let diff = n - i
if isprime[diff] {
flag = true
print("\(n) = \(i) + \(diff)")
break
}
}
}
}
if !flag {
print("Goldbach's conjecture is wrong.")
}
}
π μ΅μ’ μ λ΅μ½λ
import Foundation
private func solution() {
let isprime = primeNumArr()
for _ in 0..<100000 {
var flag = false
let n = Int(readLine()!)!
if n == 0 {
break
} else {
for i in 3...n {
if isprime[i] {
let diff = n - i
if isprime[diff] {
flag = true
print("\(n) = \(i) + \(diff)")
break
}
}
}
}
if !flag {
print("Goldbach's conjecture is wrong.")
}
}
}
private func primeNumArr() -> [Bool] {
var isprimenum = Array(repeating: true, count: 1000001)
isprimenum[0] = false
isprimenum[1] = false
for i in 2..<1000001 {
if isprimenum[i] == false { continue }
for j in stride(from: i+i, through: 1000000, by: i) {
isprimenum[j] = false
}
}
return isprimenum
}
solution()
μμ¦, μκ°λ³΅μ‘λμ λν΄μ μκ°νλ©΄μ νΈλ λΉλκ° λμ΄λκ³ μλ€. μνκ³ μλκ±°κ² μ§!! μκ³ λ¦¬μ¦ κ³ μκ° λκ² μ΄ π«’ νμ΄ν π