Last active
February 23, 2020 15:45
-
-
Save dirtyhenry/c2a3eadebf7534acbd975bf3aebeb935 to your computer and use it in GitHub Desktop.
A solution to the 711 problem: 1.2, 1.25, 1.5, 3.16
This file contains 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 | |
// Maths2 | |
// | |
// Created by Mickaël Floc'hlay on 23/02/2020. | |
// Copyright © 2020 mickf.net. All rights reserved. | |
// | |
import Foundation | |
func primeFactors(n: Int) -> [Int] { | |
var n = n | |
var factors: [Int] = [] | |
for divisor in 2 ..< n { | |
guard n > 1 else { | |
break | |
} | |
while n % divisor == 0 { | |
factors.append(divisor) | |
n /= divisor | |
} | |
} | |
return factors | |
} | |
func split(set: [Int], in nbPartitions: Int) -> [[[Int]]] { | |
debugPrint("setSize: \(set.count)") | |
guard set.count >= nbPartitions else { | |
fatalError("The set can't have less elements than the number of partitions.") | |
} | |
if set.count == nbPartitions { | |
var res: [[Int]] = [] | |
for i in stride(from: 0, to: set.count - 1, by: 1) { | |
res.append([set[i]]) | |
} | |
return [res] | |
} else { | |
var res: [[[Int]]] = [] | |
for index in stride(from: 0, to: set.count - 1, by: 1) { | |
let extractedElement = set[index] | |
var copyOfSet = Array(set) | |
copyOfSet.remove(at: index) | |
let tmpRes = split(set: copyOfSet, in: nbPartitions) | |
// Now reinject extracted element | |
let subRes: [[[Int]]] = tmpRes.reduce(into: []) { (finalResult, newSubset) in | |
// newSubset is [x11, ...], [x21, ...], ..., [xn1, ...] | |
// we must add [x11, ..., a], ... | |
// and [x11, ...), [x21, ..., a], etc. | |
for i in stride(from: 0, to: nbPartitions - 1, by: 1) { | |
var newResult = newSubset[i] | |
newResult.append(extractedElement) | |
var copyOfNewSubset = newSubset | |
copyOfNewSubset[i] = newResult | |
finalResult.append(copyOfNewSubset) | |
} | |
} | |
res.append(contentsOf: subRes) | |
} | |
return res | |
} | |
} | |
//print(primeFactors(n: 711000000)) | |
//print(split(set: primeFactors(n: 711000000), in: 4)) | |
func shuffle(set: [Int], nbPartitions: Int) -> [[Int]] { | |
let shuffledSet = set.shuffled() | |
var partitionsIndex: [Int] = [] | |
for i in 0..<nbPartitions { | |
partitionsIndex.append(i) | |
} | |
while partitionsIndex.count < shuffledSet.count { | |
partitionsIndex.append(Int.random(in: 0..<nbPartitions)) | |
} | |
partitionsIndex.shuffle() | |
var res: [[Int]] = Array(repeating: [], count: nbPartitions) | |
for (index, value) in shuffledSet.enumerated() { | |
res[partitionsIndex[index]].append(value) | |
} | |
return res | |
} | |
let problemValue = 711 | |
//let problemValue = 2009 | |
let factors = primeFactors(n: problemValue * 1000000) | |
var resultFound = false | |
var nbAttempts = 0 | |
while(!resultFound) { | |
nbAttempts += 1 | |
let newAttempt = shuffle(set: factors, nbPartitions: 4) | |
var humanReadableAnswer: [Int] = [] | |
let newResult = newAttempt.reduce(0) { (a, newSet) -> Int in | |
let b = newSet.reduce(1) { (x, y) in | |
return x * y | |
} | |
humanReadableAnswer.append(b) | |
return a + b | |
} | |
if newResult == problemValue { | |
print("🎉: \(humanReadableAnswer) after \(nbAttempts) attempts") | |
resultFound = true | |
} | |
} |
This file contains 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
ArraySlice([__lldb_expr_1.Answer(tuple: (1.2, 1.25, 1.5, 3.16), epsilon: 0.0), __lldb_expr_1.Answer(tuple: (1.2, 1.25, 3.16, 1.5), epsilon: 0.0), __lldb_expr_1.Answer(tuple: (1.2, 1.5, 3.16, 1.25), epsilon: 8.881784197001252e-16), __lldb_expr_1.Answer(tuple: (1.25, 1.5, 3.16, 1.2000000000000002), epsilon: 1.7763568394002505e-15), __lldb_expr_1.Answer(tuple: (1.6600000000000001, 1.6900000000000002, 2.8800000000000003, 0.8799999999999994), epsilon: 5.759999997856369e-06), __lldb_expr_1.Answer(tuple: (0.78, 2.08, 2.49, 1.7599999999999998), epsilon: 5.7599999996327256e-06)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment