How to resolve the algorithm Deceptive numbers step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Deceptive numbers step by step in the OCaml programming language
Table of Contents
Problem Statement
Repunits are numbers that consist entirely of repetitions of the digit one (unity). The notation Rn symbolizes the repunit made up of n ones. Every prime p larger than 5, evenly divides the repunit Rp-1.
The repunit R6 is evenly divisible by 7. 111111 / 7 = 15873 The repunit R42 is evenly divisible by 43. 111111111111111111111111111111111111111111 / 43 = 2583979328165374677002583979328165374677 And so on.
There are composite numbers that also have this same property. They are often referred to as deceptive non-primes or deceptive numbers.
The repunit R90 is evenly divisible by the composite number 91 (=7*13).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Deceptive numbers step by step in the OCaml programming language
Source code in the ocaml programming language
let modpow m =
let rec loop p b e =
if e land 1 = 0
then if e = 0 then p else loop p (b * b mod m) (e lsr 1)
else loop (p * b mod m) (b * b mod m) (e lsr 1)
in loop 1
let is_deceptive n =
let rec loop x =
x * x <= n && (n mod x = 0 || n mod (x + 4) = 0 || loop (x + 6))
in
n land 1 <> 0 && n mod 3 <> 0 && n mod 5 <> 0 &&
modpow n 10 (pred n) = 1 && loop 7
let () =
Seq.(ints 49 |> filter is_deceptive |> take 100
|> iter (Printf.printf " %u%!")) |> print_newline
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Elena programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Ruby programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Phix programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the AWK programming language