Project Euler -- 欧拉题集 F#(Fsharp)及Haskell 版 - No.3, No.4
来源:互联网 发布:怎么破解apk软件 编辑:程序博客网 时间:2024/06/05 06:08
No3.
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
F#: -- F#需要explicitly告知是UL (uint64_t)
let biggestPrimeFactor (x:uint64) =
let rec bpfAux (x:uint64) (y:uint64) =
if x = y then x
elif x % y = 0UL then bpfAux (x / y) y
else bpfAux x (y + 1UL)
bpfAux x 2UL
let anwser = biggestPrimeFactor 600851475143UL
Haskell:
biggestPrimeFactor x = bpfAux x 2
where bpfAux a b | a == b = a
| a `mod` b == 0 = bpfAux (a `div` b) b
| otherwise = bpfAux a (b+1)
anwser = biggestPrimeFactor 600851475143
Anwser: 6857
后语: 这题不能暴力破解啊,开始时候简单的用 isPrime, isFactor这样一个个数字去判断,结果一个晚上没算出来。
现在用的是 functor tree, 即:
60 / \ 2 30 / \ 2 15 / \ 3 5
线性时间。
No4.
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
F#:
let isPalindrome n =
let nStr = string n |> List.ofSeq
nStr = List.rev nStr
let biggestPalindrome3 =
let max = ref 0
let rec bpAux a b c =
if a = c then max
elif isPalindrome (a * b) && (a*b > !max) then max := (a*b); bpAux a (b-1) c
elif b <= c then bpAux (a-1) (a-1) c
else bpAux a (b-1) c
bpAux 999 999 100
Haskell:
in reverse xText == xText
biggestPalindrom n = let palin = bpAux ( (read . take n $ cycle "9") :: Integer ) (10 ^ (n-1))
in maximum palin
where bpAux x y = aux2 x x y
aux2 a b c | a == c = []
| isPalindrome (a*b) = (a*b) : aux2 a (b-1) c
| b <= c = aux2 (a-1) (a-1) c
| otherwise = aux2 a (b-1) c
anwser4 = biggestPalindrom 3
Anwser: 906609
后语:”biggestPalindrom n“ 的n表示是几位的数字,n=5时,这个计算就很慢了,需换算法。
- Project Euler -- 欧拉题集 F#(Fsharp)及Haskell 版 - No.3, No.4
- Project Euler -- 欧拉题集 F#(Fsharp)及Haskell 版 - No.1, No.2
- Project Euler -- 欧拉题集 F#及Haskell 版 - No.5, No.6
- Project Euler -- 欧拉题集 F#及Haskell 版 - No.7, No.8
- Project Euler -- 欧拉题集 F#及Haskell 版 - No.9, No.10
- Project Euler. No.1
- <MEMORY>Project Euler NO.12
- for no f test
- GitLab: No such project
- Project Euler - Problem 4
- Project Euler problem 4
- Project Euler 4
- Project Euler 4
- Project Euler - 4
- Project Euler Problem 4
- Project Euler Question 4
- Project Euler Problem 4
- project euler 4
- Objective-C语法之NSString字符串的那些事儿(三)
- 公司一同事写的存储过程 带游标
- ubuntu timezone setting
- Dynamics CRM 2011中常用的辅助工具
- 人脸识别(不是检测,而是识别,源码下载)
- Project Euler -- 欧拉题集 F#(Fsharp)及Haskell 版 - No.3, No.4
- 嵌入式boa服务器搭建和移植
- iWitness:基于时间和地理位置显示Twitter和Flickr内容
- Perl组合技:sub
- 关于YII中JSON的操作
- 为什么需要字节对齐?
- rs 实用工具 (rs.exe) (SSRS)SQL server report service
- SQL Server 2008 压缩
- java 变量级别及action向jsp传值[整理]