1 people like it.

euler047.fsx

Jelmer047.fsx

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
(* Project Euler Problem 47: Distinct primes factors

The first two consecutive numbers to have two distinct prime factors are:

14 = 2 × 7
15 = 3 × 5

The first three consecutive numbers to have three distinct prime factors
are:

644 = 2² × 7 × 23
645 = 3 × 5 × 43
646 = 2 × 17 × 19.

Find the first four consecutive integers to have four distinct prime
factors. What is the first of these numbers? *)

let isprime n = (n=2) || not (List.exists (fun d -> n%d=0) ([2]@[3..2..(int(sqrt(float n)))]))

let primes = [|for n in 2..2999 do if (isprime n) then yield n|]

let pfs x = Array.length (Array.filter (fun d -> x%d=0) (Array.takeWhile (fun p -> p < x/2) primes))

let pfsxyz x = ((pfs (x) = 4) && (pfs (x+1) = 4) && (pfs (x+2) = 4) && (pfs (x+3) = 4))

let consecutives upto = [for n in 1..upto do if (pfsxyz n) then yield n]

printfn "%O" (consecutives 150000)
val isprime : n:int -> bool

Full name: Script.isprime
val n : int
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val exists : predicate:('T -> bool) -> list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.exists
val d : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val primes : int []

Full name: Script.primes
val pfs : x:int -> int

Full name: Script.pfs
val x : int
module Array

from Microsoft.FSharp.Collections
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
val filter : predicate:('T -> bool) -> array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.filter
val pfsxyz : x:int -> bool

Full name: Script.pfsxyz
val consecutives : upto:int -> int list

Full name: Script.consecutives
val upto : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/sZ
Posted:8 years ago
Author:jelmer
Tags: f# , fsharp , euler , tryfsharp