Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Kaprekar-numbers/F-Sharp/kaprekar-numbers.fs
Normal file
35
Task/Kaprekar-numbers/F-Sharp/kaprekar-numbers.fs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
// Count digits in number
|
||||
let digits x =
|
||||
let rec digits' p x =
|
||||
if 10.**p > x then p else digits' (p + 1.) x
|
||||
digits' 1. x
|
||||
|
||||
|
||||
// Is n a Kaprekar number?
|
||||
let isKaprekar n =
|
||||
// Reference: http://oeis.org/A006886
|
||||
// Positive numbers n such that n=q+r
|
||||
// And n^2=q*10^m+r,
|
||||
// for some m >= 1,
|
||||
// q>=0 and 0<=r<10^m,
|
||||
// with n != 10^a, a>=1.
|
||||
let nSquared = n * n
|
||||
let a = float((digits n) - 1.)
|
||||
|
||||
// Create a list of tuples from the nSquared digit splits
|
||||
[1. .. float (digits nSquared)]
|
||||
|> List.map (fun e ->
|
||||
// Splits the nSquared digits into 2 parts
|
||||
let x = 10.**e
|
||||
let q = float(int(Math.Floor (nSquared / x)))
|
||||
let r = nSquared - (q * x)
|
||||
(q, r))
|
||||
// Filter results based on rules
|
||||
|> List.exists (fun (q, r) ->
|
||||
q + r = n &&
|
||||
if a >= 1. then n % 10.**a <> 0. else true)
|
||||
|
||||
|
||||
// List Kaprekar numbers from 1 to 10,000
|
||||
[1 .. 10000]
|
||||
|> List.filter (float >> isKaprekar)
|
||||
Loading…
Add table
Add a link
Reference in a new issue