Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1929๋ฒˆ - ์†Œ์ˆ˜์ฐพ๊ธฐ (๋˜ ๋“ฑ์žฅํ•œ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด)

๊ฐ์ž ๐Ÿฅ” 2022. 12. 27. 15:15
๋ฐ˜์‘ํ˜•

์•„์ง๋„ ์†Œ์ˆ˜๋ž‘ ์ „์Ÿ์ค‘! ์—ญ์‹œ๋‚˜ ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ๊ณ ์ƒ์ค‘์ด๋‹ค. ๊ตฌํ˜„ํ•˜๋Š”๊ฑด ์‰ฝ์ง€๋งŒ, ์ด์   ๊ทธ ์ด์ƒ์˜ ๋‹จ๊ณ„๋ฅผ ์Šค์Šค๋กœ ํŒŒํ—ค์ณ๋ณด๊ณ  ๊นจ๋‹ซ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ด์•ผํ•œ๋‹ค. ์ฐธ ์–ด๋ ต๊ตฐ,,

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/1929 

 

1929๋ฒˆ: ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ M๊ณผ N์ด ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. (1 ≤ M ≤ N ≤ 1,000,000) M์ด์ƒ N์ดํ•˜์˜ ์†Œ์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์žˆ๋Š” ์ž…๋ ฅ๋งŒ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ํ’€์ด - ์‹œ๊ฐ„์ดˆ๊ณผ

private func solution() {
    let nm = readLine()!.split(separator: " ").map{ Int($0)! }
    for i in nm[0]...nm[1]{
        var flag = false
        for j in 2..<i {
            if i % j == 0 {
                flag = true
            }
        }
        if !flag {
            print(i)
        }
    }
}

solution()

2๋ถ€ํ„ฐ i๊นŒ์ง€ ๋ชจ๋‘ for๋ฌธ์„ ๋Œ๋ ธ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ ์ด์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์™€ ๋‹ฌ๋ฆฌ, ๋ณด์ง€ ๋ชปํ•œ ์กฐ๊ฑด์ด ์žˆ์—ˆ๋‹ค. ๋ฐ”๋กœ,, ์ž์—ฐ์ˆ˜ M๊ณผ N ์€ 1000000๊นŒ์ง€,,,, ์ฃผ์–ด์ง„๋‹ค๋Š” ์‚ฌ์‹ค... ์ด๊ฒƒ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€ ์•Š์•˜์„๊นŒ? 2์ดˆ ์ œํ•œ์ด ๊ต‰์žฅํžˆ ํฐ ์ค„ ์•Œ์•˜๋Š”๋ฐ, ์ง€๋‚œ๋ฒˆ ๋ฌธ์ œ์™€ ๋‹ฌ๋ฆฌ ์ž์—ฐ์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ ํ›จ์”ฌ ์ปค์กŒ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ์ฝ” ๊ธด ์‹œ๊ฐ„์€ ์•„๋‹ˆ์—ˆ๋‚˜๋ณด๋‹ค.
(๋Œ€์ฒด ๋ฐฑ์ค€์—์„œ ์ด '๊ธธ๋‹ค', '์งง๋‹ค'์˜ ํŒ๋‹จ์€ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”๊ฑธ๊นŒ?,, ์ด์ œ์•ผ ๋Œ€์ถฉ ์‹œ๊ฐ„๋ณต์žก๋„์˜ ๋น…Oํ‘œ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” ์ตํ˜”๋Š”๋ฐใ… ใ… ,, )

๐Ÿ™‹๐Ÿป‍โ™€๏ธ ์‹œ๊ฐ„์„ ์žฌ๋ดค๋‹ค.

์ด์ „ ํฌ์ŠคํŒ…์—์„œ ์‹คํ–‰์†๋„ ๊ด€๋ จํ•ด์„œ ๋ธ”๋กœ๊ทธ์— ์งˆ๋ฌธ์„ ๋‚จ๊ฒผ์—ˆ๋‹ค. ๊ฐ์‚ฌํ•˜๊ฒŒ๋„, iOS ๊ฒ€์ƒ‰ํ•˜๋‹ค๋ณด๋ฉด ๋งŽ์ด ๋‚˜์˜ค๋Š”,, ์ •์ฃผ๋‹˜๊ป˜์„œ ๋‹ต๋ณ€์„ ์ฃผ์…จ๋‹ค. ๋ฐฑ์ค€์€ ์‹คํ–‰์†๋„ ์ธก์ •์ด ์ •ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ํ•ด๋ด์•ผํ•œ๋‹ค๊ณ  ํ•˜์…จ๋‹ค. ๐Ÿ˜‡ ์ •์ฃผ๋‹˜,,, ์ฒœ์‚ฌ,,,,

๊ทธ๋ž˜์„œ ์•ž์œผ๋กœ๋Š” ์ง์ ‘ ์‹คํ–‰์†๋„๋ฅผ ์ธก์ •ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค!! 

(์ธก์ •ํ•ด๋ณด๋‹ˆ,,) ๋ง๋„์•ˆ๋ผ,, i ๋ฅผ ๊ฑฐ์˜ ์ตœ๋Œ€์ˆซ์ž 990000์œผ๋กœ ์ฃผ๊ณ  ๋Œ๋ ค๋ณด์•˜๋‹ค. ๋น„๊ต๋ฅผ ์œ„ํ•ด์„œ, ์ง€๋‚œ๋ฒˆ์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์™€ ๊ฐ™์ด '์ œ๊ณฑ๊ทผ'์„ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ ํ•จ๊ป˜ ๋น„๊ตํ•ด๋ณด์•˜๋‹ค. ๋๋‚˜์ง€ ์•Š๋Š”๋‹ค.. xcode..์ด์ œ๋ฉˆ์ถฐ์ค˜ ์ด ๋ฐฉ๋ฒ•๋ง๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•ด๋ด์•ผ๊ฒ ๋‹ค..

import Foundation

public func progressTime(_ closure: () -> ()) -> TimeInterval {
    let start = CFAbsoluteTimeGetCurrent()
    closure()
    let diff = CFAbsoluteTimeGetCurrent() - start
    return (diff)
}

private func solution() {
    let nm = readLine()!.split(separator: " ").map{ Int($0)! }
    for i in nm[0]...nm[1]{
        var flag = false
        for j in 2..<i {
            if i % j == 0 {
                flag = true
            }
        }
        if !flag {
//            print(i)
        }
    }
}

private func solution2() {
    let nm = readLine()!.split(separator: " ").map{ Int($0)! }
    for i in nm[0]...nm[1]{
        var flag = false
        for j in 2..<Int(sqrt(Double(i))) + 1 {
            if i % j == 0 {
                flag = true
            }
        }
        if !flag {
//            print(i)
        }
    }
}

print(progressTime {
    solution()
})

print(progressTime {
    solution2()
})

๋๋‚˜์ง€ ์•Š๋Š”๋‹ค!!!

๐ŸŸ  ๋‘๋ฒˆ์งธ ํ’€์ด - ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๊ธฐ - ๋งž์•˜์Šต๋‹ˆ๋‹ค!

์ง์ ‘ xcode์—์„œ ์—ฐ์‚ฐ์„ ํ•ด๋ณด๋‹ˆ ์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ์—์„œ ์ž˜๋ชป๋œ ๋ถ€๋ถ„์„ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. nm[0] ๋ถ€ํ„ฐ nm[1]๊นŒ์ง€ ๋ชจ๋“  ์ˆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ํ•œ๋ฒˆ์”ฉ ๋ชจ๋‘ ์†Œ์ˆ˜ ํŒ๋ณ„์„ ๊ฑฐ์นœ๋‹ค๋Š” ์ ์ด ์ž˜๋ชป๋๋‹ค. ๋งŒ์•ฝ, nm์ด ๊ฐ€์žฅ ๋„“์€ ๋ฒ”์œ„์ธ, 1๋ถ€ํ„ฐ 100๋งŒ๊นŒ์ง€ ๊ฐ„๋‹ค๊ณ  ์ณ๋ณด์ž. ๊ทธ๋Ÿผ ๋ฐฑ๋งŒ๋ฒˆ์˜ ์†Œ์ˆ˜ ํŒ๋ณ„์ด ์ด๋ฃจ์–ด์ ธ์•ผํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ์—ฐ์‚ฐ์„ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ผ ๊ฒƒ์ด๋‹ค.

์ด์ „ ํ’€์ด์—์„œ ์ตํ˜”๋˜, ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ๋ฐฉ์‹์„ ์ด์šฉํ•ด๋ณด์ž. 100๋งŒ๊นŒ์ง€ ๋ชจ๋“  ์ˆ˜์— ๋Œ€ํ•œ ์†Œ์ˆ˜ ํŒ๋ณ„์„ ๋ฏธ๋ฆฌ ํ•ด๋‘” ๋’ค, ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜์— ๋Œ€ํ•ด์„œ ์†Œ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋งŒ ํŒ๋‹จํ•ด์„œ ๋ฐ”๋กœ ์ถœ๋ ฅํ•ด์ฃผ์ž.

์ด ๋ฐฉ๋ฒ•์€ ์‹ค์ œ ์‹คํ–‰์†๋„๋ฅผ ์ธก์ •ํ•ด๋ณด๋‹ˆ, ์ ์€ ๋ฒ”์œ„์—์„œ๋Š” ์‹คํ–‰์†๋„์˜ ์ฐจ์ด๊ฐ€ ๋‚˜์ง€ ์•Š์•˜์ง€๋งŒ ์—ญ์‹œ ๋ฒ”์œ„๊ฐ€ ์ปค์กŒ์„๋•Œ ํšจ์œจ์ ์ธ ๊ฒƒ ๊ฐ™์•˜๋‹ค!! 

private func solution3() {
    let nm = readLine()!.split(separator: " ").map{ Int($0)! }
    var isprimenum = Array(repeating: true, count: 1000001)
    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
        }
    }

    for k in nm[0]...nm[1] {
        if isprimenum[k] == true {
            print(k)
        }
    }
}

solution3()

์งœ์ž” ~! ๋งž์•˜๋‹ค. ์•„, ์™œ ์ฒซ์‹œ๋„๋Š” ํ‹€๋ ธ๋‚˜๋ฉด, 1์„ ์ฒ˜๋ฆฌํ•ด์ฃผ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ~ ๐Ÿ˜‰ ๊ธฐ์–ตํ•˜์„ธ์š” ์—ฌ๋Ÿฌ๋ถ„ 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค! 

 

๋ฐ˜์‘ํ˜•