F#。由于解决了Euler#3问题而超时而终止

时间:2019-03-05 12:48:34

标签: algorithm f# primes prime-factoring

我告诉了这个问题:https://www.hackerrank.com/contests/projecteuler/challenges/euler003

我正在尝试解决以下问题:

open System


let isPrime n =
    match n with
    | _ when n > 3L && (n % 2L = 0L || n % 3L = 0L) -> false
    | _ ->
        let maxDiv = int64(System.Math.Sqrt(float n)) + 1L
        let rec f d i = 
            if d > maxDiv then 
                true
            else
                if n % d = 0L then 
                    false
                else
                    f (d + i) (6L - i)     
        f 5L 2L

let primeFactors n =
    let rec getFactor num proposed acc =
        match proposed with
        | _ when proposed = num -> proposed::acc
        | _ when num % proposed = 0L -> getFactor (num / proposed) proposed (proposed::acc)
        | _ when isPrime num -> num::acc
        | _ -> getFactor num (proposed + 1L) acc
    getFactor n 2L []


let pe3() =
    for i = 1 to Console.ReadLine()  |> int  do
        let num = Console.ReadLine() |> int64
        let start = DateTime.Now
        primeFactors num 
            |> List.max
            |> printfn "%i" 
        let elapsed = DateTime.Now - start
        printfn "elapsed: %A" elapsed

pe3()

我的测试结果如下:

  • 输入: 10 输出: 5 经过时间: 00:00:00.0562321

  • 输入: 123456789 输出: 3803 经过时间: 00:00:00.0979232

  • 输入: 12345678999 输出: 1371742111 经过时间: 00:00:00.0520280

  • 输入: 987654321852 输出: 680202701 经过时间: 00:00:00.0564059

  • 输入: 13652478965478 输出: 2275413160913 经过时间: 00:00:00.0593369

  • 输入: 99999999999999 输出: 909091 经过时间: 00:00:00.1260673

但是由于测试案例5 中的超时,我还是被终止了。我该怎么办?

2 个答案:

答案 0 :(得分:1)

有解决方案:

open System

let primeFactors n =
    let rec getFactor num proposed acc =
       match proposed with
       | _ when proposed*proposed > num -> num::acc
       | _ when num % proposed = 0L -> getFactor (num / proposed) proposed (proposed::acc)
       | _ -> getFactor num (proposed + 1L) acc
    getFactor n 2L []

let pe3() =
    for i = 1 to Console.ReadLine()  |> int  do
        printfn "%i" (primeFactors (Console.ReadLine() |> int64)).Head
pe3()

感谢 Will Ness rici

答案 1 :(得分:0)

There is no need to write super sophisticated code for this challenge. A simple algorithm to enumerate a number's prime factors will do the trick. My code creates a seq of the prime factors, then finds the maximum, and prints it. The rest of the code shows a nice functional way of handling processing of lines read from standard input.

module Auxiliaries =

    let isNull (x : 'a when 'a : not struct) =
        match box x with
        | null -> true
        | _ -> false

    let refAsOption x =
        if isNull x then None else Some x

    let readLinesFromTextReader r =
        let tryRdLn (r : System.IO.TextReader) =
            try refAsOption (r.ReadLine ()) with _ -> None
        let gen r =
            tryRdLn r |> Option.map (fun s -> (s, r))
        Seq.unfold gen r

module Contest =

    let factors num =
        let next n =
            if n = 2L then 3L
            elif n % 6L = 1L then n + 4L
            else n + 2L
        let rec loop nn ct lf =
            seq {
                if ct * ct > nn then
                    if nn > lf then yield nn
                elif nn % ct = 0L then
                    yield ct
                    yield! loop (nn / ct) ct ct
                else
                    yield! loop nn (next ct) lf
            }
        loop num 2L 0L

    let euler003 n = factors n |> Seq.max

    let () =
        Auxiliaries.readLinesFromTextReader stdin
        |> Seq.skip 1
        |> Seq.map (int64 >> euler003)
        |> Seq.iter stdout.WriteLine