Last active
August 25, 2017 00:17
-
-
Save stripe-q/7e69d01c0096f586e9fad87a174ee1a9 to your computer and use it in GitHub Desktop.
프로젝트 오일러 47번 풀이
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// Euler-47 | |
// | |
// Created by sooop on 2017. 8. 25.. | |
// Copyright © 2017년 sooop. All rights reserved. | |
// | |
import Foundation | |
func factors(_ m:Int) -> Set<Int> { | |
var a = 1 | |
var p = Set<Int>() | |
var n = m | |
func divisor(over a: Int) -> Int { | |
switch a { | |
case (1...2): | |
if n % (a + 1) == 0 { return a + 1} | |
return divisor(over: a + 1) | |
case 3: | |
if n % 5 == 0 { return 5 } | |
return divisor(over: 5) | |
default: | |
var k = a | |
var i = 0 | |
let ds = (k % 3 == 1) ? [4, 2] : [2, 4] | |
let l = Int(sqrt(Double(n)) + 0.5) | |
while k <= l { | |
if n % k == 0 { return k } | |
k += ds[i] | |
i = (i + 1) % 2 | |
} | |
return n | |
} | |
} | |
while n > 1 { | |
a = divisor(over: a) | |
p.insert(a) | |
while n % a == 0 { | |
n /= a | |
} | |
} | |
return p | |
} | |
func solve(){ | |
var s = 2*3*5*7 + 1 | |
var f0: Set<Int> = [2,3,5,7] | |
var c = 0 | |
while true { | |
let f1 = factors(s) | |
if f1.count == 4 && f1.intersection(f0).isEmpty { | |
c += 1 | |
if c > 3 { | |
print(s - 3) | |
return | |
} | |
} else { | |
c = 0 | |
} | |
f0 = f1 | |
s += 1 | |
} | |
} | |
func timeit(_ f: () -> ()) { | |
let s = Date() | |
f() | |
print(s.timeIntervalSinceNow * -1000) | |
} | |
timeit(solve) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment