1 people like it.

Luhn algorithm

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
open System.Text.RegularExpressions
open System

let input = "378282246310005"

let digits = input |> Seq.filter System.Char.IsNumber |> Seq.map (string >> int)
if (digits |> Seq.length) <> 0 then
    let multiply i n = 
        match i % 2 with 
        | 1 when n < 5 -> n * 2 
        | 1 -> (n * 2) - 9 
        | _ -> n
    let sum = digits |> Seq.rev |> Seq.mapi multiply |> Seq.sum
    sum % 10 = 0   
else
    false
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
val input : string

Full name: Script.input
val digits : seq<int>

Full name: Script.digits
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.IsNumber(c: char) : bool
Char.IsNumber(s: string, index: int) : bool
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
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 length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val multiply : (int -> int -> int)
val i : int
val n : int
val sum : int
val rev : source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.rev
val mapi : mapping:(int -> 'T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
Raw view New version

More information

Link:http://fssnip.net/7Wo
Posted:1 year ago
Author:Marnee Dearman
Tags: luhn