- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个问题包含剧透给那些没有完成的人problem 61 of Project Euler .我写了一个必要的问题的答案,所以我开始做一个更通用、更实用的答案。我成功了,但现在正试图弄清楚如何将其重构为或使用计算表达式,并且感到无可救药地困惑。下面详细描述了该问题,但要点是您正在尝试建立一个数字链,当按顺序放置时,这些数字链将显示所有相邻对的属性。每个链的候选者都来自不同的数字池,这意味着蛮力算法必须聪明,以避免需要搜索每个可能的排列。
我对包含计算表达式的猜测是以某种方式将搜索算法变成一个 monad,它继续添加到解决方案或转储空列表。但我不完全确定。
(*
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are
all figurate (polygonal) numbers and are generated by the following formulae:
Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, ...
Square P4,n=n2 1, 4, 9, 16, 25, ...
Pentagonal P5,n=n(3n−1)/2 1, 5, 12, 22, 35, ...
Hexagonal P6,n=n(2n−1) 1, 6, 15, 28, 45, ...
Heptagonal P7,n=n(5n−3)/2 1, 7, 18, 34, 55, ...
Octagonal P8,n=n(3n−2) 1, 8, 21, 40, 65, ...
The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three
interesting properties.
The set is cyclic, in that the last two digits of each number is the first two
digits of the next number (including the last number with the first).
Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal
(P5,44=2882), is represented by a different number in the set.
This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which
each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and
octagonal, is represented by a different number in the set.
*)
let rec distribute e = function
| [] -> [[e]]
| x::xs' as xs -> (e::xs)::[for xs in distribute e xs' -> x::xs]
// Return a list of all permutations of a list
let rec permute = function
| [] -> [[]]
| e::xs -> List.collect (distribute e) (permute xs)
// Return a list rotated until it's minimum element is the head
let canonicalCyclicPermutation (permutationList : 'a list) =
let min = Seq.min permutationList
let rec loop ourList =
match ourList with
| head :: tail when head = min -> ourList
| head :: tail -> loop (tail @ [head])
loop permutationList
// Return a list of all permutations of a list that is rotationally/cylically unique
let permutateCycUniq seedList =
permute seedList
|> List.distinctBy canonicalCyclicPermutation
// Generate a sequence of all s-gonal numbers
let polygonalGenerator s =
Seq.initInfinite (fun idx -> ((pown (idx+1) 2) * (s-2) - (idx+1)*(s-4))/2)
// Generate a sequence of s-gonal numbers relevant for our exercise
let polygonalCandidates s =
s
|> polygonalGenerator
|> Seq.skipWhile (fun x -> x <= 999)
|> Seq.takeWhile (fun x -> x <= 9999)
|> Seq.cache
// Create the polygonal numbers as a list not seq
let polygonalCandidatesL s =
polygonalCandidates s
|> Seq.toList
// Returns true if the last digits of first input are first digits in last input
let sharesDigits xxvv vvyy =
(xxvv / 100) = (vvyy % 100)
// Returns true if a sequence is cyclical
let isCyclical intSeq =
(Seq.append intSeq (Seq.take 1 intSeq))
|> Seq.pairwise
|> Seq.fold (fun acc (num1,num2) -> acc && (sharesDigits num1 num2)) true
// Returns an empty list if the candidate number does not share digits
// with the list head, otherwise returns the list with the candidate at the head
let addCandidateToSolution (solution : int list) (number : int) =
match solution with
| (head::tail) when sharesDigits number head -> number::head::tail
| _ -> []
// Returns a sequence of all valid solutions generated by trying to add
// a sequence of candidates to all solutions in a sequence
let addCandidatesToSolution (solutions : int list seq) (candidates : int seq) =
Seq.collect (fun solution ->
Seq.map (fun candidate ->
addCandidateToSolution solution candidate)
candidates
|> Seq.filter (not << List.isEmpty))
solutions
// Given a list of side lengths, we return a sequence of cyclical solutions
// from the polygonal number families in the order they appear in the list
let generateSolutionsFromPolygonalFamilies (seedList : int list) =
let solutionSeeds =
seedList
|> List.head
|> polygonalCandidates
|> Seq.map (fun x -> [x])
let solutions =
Seq.fold (fun acc elem -> (addCandidatesToSolution acc elem))
solutionSeeds
((List.tail seedList) |> List.map polygonalCandidatesL)
|> Seq.filter isCyclical
solutions
// Find all cyclical sequences from a list of polygonal number families
let FindSolutionsFromFamilies intList =
intList
|> permutateCycUniq
|> Seq.collect generateSolutionsFromPolygonalFamilies
|> Seq.toList
// Given in the problem
let sampleAnswer = FindSolutionsFromFamilies [3;4;5]
// The set of answers that answers the problem
#time
let problemAnswer = FindSolutionsFromFamilies [3 .. 8]
#time // 0.09s wow!
最佳答案
虽然起初持怀疑态度,但我不得不承认这个问题背后的想法非常合理,而实际的实现似乎很难实现。由于需要为给定的数据结构提供 member Bind 的等效单子(monad)签名:ma:'a list * f:('a -> 'b list) -> 'b list
,它只是坚持使用 F# list 并使用其相应的高阶函数 List.collect
是很自然的。
type ListBuilder () =
member __.Return x = [x]
member __.Bind(ma, f) = List.collect f ma
let myList a b = ListBuilder () {
let! x = a
let! y = b
return x, y }
myList [1..2] [3..4] // [(1, 3); (1, 4); (2, 3); (2, 4)]
这个最小但巧妙的笛卡尔积并没有让我们走得太远。我们需要使链条下的执行有条件,需要一个额外的成员,Zero
。显然,固定数量是这种方法的主要缺点。
type MyListBuilder () =
member __.Zero _ = []
member __.Return x = [x]
member __.Bind(ma, f) = List.collect f ma
let myListXY cmp a b c = MyListBuilder () {
let! r = a
let! s = b
if cmp r s then
let! t = c
if cmp s t then
if cmp t r then
return r, s, t }
let T n k = if n < 2 then 0 else ((n - 2) * k * k - (n - 4) * k) / 2
let figurate s min max =
Seq.initInfinite ((+) 1)
|> Seq.map (T s)
|> Seq.takeWhile (fun n -> n < max)
|> Seq.filter (fun n -> n >= min)
|> Seq.toList
myListXY (fun x y -> x % 100 = y / 100)
(figurate 3 1000 10000)
(figurate 5 1000 10000)
(figurate 4 1000 10000) // [(8128, 2882, 8281)]
关于algorithm - 重构算法作为计算表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47119481/
我在一本书(Interview Question)中读到这个问题,想在这里详细讨论这个问题。请点亮它。 问题如下:- 隐私和匿名化 马萨诸塞州集团保险委员会早在 1990 年代中期就有一个绝妙的主意
我最近接受了一次面试,面试官给了我一些伪代码并提出了相关问题。不幸的是,由于准备不足,我无法回答他的问题。由于时间关系,我无法向他请教该问题的解决方案。如果有人可以指导我并帮助我理解问题,以便我可以改
这是我的代码 public int getDist(Node root, int value) { if (root == null && value !=0) return
就效率而言,Strassen 算法应该停止递归并应用乘法的最佳交叉点是多少? 我知道这与具体的实现和硬件密切相关,但对于一般情况应该有某种指南或某人的一些实验结果。 在网上搜索了一下,问了一些他们认为
我想学习一些关于分布式算法的知识,所以我正在寻找任何书籍推荐。我对理论书籍更感兴趣,因为实现只是个人喜好问题(我可能会使用 erlang(或 c#))。但另一方面,我不想对算法进行原始的数学分析。只是
我想知道你们中有多少人实现了计算机科学的“ classical algorithms ”,例如 Dijkstra's algorithm或现实世界中的数据结构(例如二叉搜索树),而不是学术项目? 当有
我正在解决旧编程竞赛中的一些示例问题。在这个问题中,我们得到了我们有多少调酒师以及他们知道哪些食谱的信息。制作每杯鸡尾酒需要 1 分钟,我们需要使用所有调酒师计算是否可以在 5 分钟内完成订单。 解决
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我开始学习 Nodejs,但我被困在中间的某个地方。我从 npm 安装了一个新库,它是 express -jwt ,它在运行后显示某种错误。附上代码和错误日志,请帮助我! const jwt = re
我有一个证书,其中签名算法显示“sha256rsa”,但指纹算法显示“sha1”。我的证书 SHA1/SHA2 的标识是什么? 谢谢! 最佳答案 TL;TR:签名和指纹是完全不同的东西。对于证书的强度
我目前在我的大学学习数据结构类(class),并且在之前的类(class)中做过一些算法分析,但这是我在之前的类(class)中遇到的最困难的部分。我们现在将在我的数据结构类(class)中学习算法分
有一个由 N 个 1x1 方格组成的区域,并且该区域的所有部分都是相连的(没有任何方格无法到达的方格)。 下面是一些面积的例子。 我想在这个区域中选择一些方块,并且两个相邻的方块不能一起选择(对角接触
我有一些多边形形状的点列表,我想将其包含在我页面上的 Google map 中。 我已经从原始数据中删除了尽可能多的不必要的多边形,现在我剩下大约 12 个,但它们非常详细以至于导致了问题。现在我的文
我目前正在实现 Marching Squares用于计算等高线曲线,我对此处提到的位移位的使用有疑问 Compose the 4 bits at the corners of the cell to
我正在尝试针对给定算法的约束满足问题实现此递归回溯函数: function BACKTRACKING-SEARCH(csp) returns solution/failure return R
是否有包含反函数的库? 作为项目的一部分,我目前正在研究测向算法。我正在使用巴特利特相关性。在 Bartlett 相关性中,我需要将已经是 3 次矩阵乘法(包括 Hermitian 转置)的分子除以作
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
问题的链接是UVA - 1394 : And There Was One . 朴素的算法是扫描整个数组并在每次迭代中标记第 k 个元素并在最后停止:这需要 O(n^2) 时间。 我搜索了一种替代算法并
COM 中创建 GUID 的函数 (CoCreateGUID) 使用“分散唯一性算法”,但我的问题是,它是什么? 谁能解释一下? 最佳答案 一种生成 ID 的方法,该 ID 具有一定的唯一性保证,而不
在做一个项目时我遇到了这个问题,我将在这个问题的实际领域之外重新措辞(我想我可以谈论烟花的口径和形状,但这会使理解更加复杂).我正在寻找一种(可能是近似的)算法来解决它。 我有 n 个不同大小的容器,
我是一名优秀的程序员,十分优秀!