How to resolve the algorithm Deceptive numbers step by step in the Ruby programming language
How to resolve the algorithm Deceptive numbers step by step in the Ruby 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 Ruby programming language
Ruby Code to Generate Deceptive Numbers:
1. Overview:
- This code generates a sequence of "deceptive numbers," which are numbers that look prime but are actually composite and have a special property involving repunits (numbers consisting of repeated 1s).
2. Implementation Details:
-
'require 'prime': Imports the Prime module to easily check for prime numbers.
-
'deceptives = Enumerator.new do |y|': Creates an enumerator that will lazily generate deceptive numbers.
-
'10.step(by: 10) do |n|': Iterates from 10 to infinity in increments of 10.
-
'[1,3,7,9].each do |digit|': Loops over the digits 1, 3, 7, and 9.
-
'cand = n + digit': Calculates the candidate number as the original number plus the current digit.
-
'next if cand % 3 == 0 || cand.prime?': Filters out candidates that are divisible by 3 or prime.
-
'repunit = ("1"*(cand-1)).to_i': Calculates the repunit number with (cand-1) 1s.
-
'y << cand if (repunit % cand) == 0': Checks if the repunit is divisible by the candidate number. If true, the candidate number is added to the enumerator.
-
'p deceptives.take(25).to_a': Prints the first 25 deceptive numbers to the console.
3. Explanation:
-
The code generates deceptive numbers by adding digits (1, 3, 7, 9) to the numbers 10, 20, 30, etc. and checking if the resulting number satisfies the following conditions:
- Not divisible by 3.
- Not prime.
- The repunit with (cand-1) 1s is divisible by the candidate number.
-
Numbers meeting these criteria have the deceptive property that they appear prime but are composite.
4. Output:
The output will be a list of the first 25 deceptive numbers, which are:
[31, 73, 79, 223, 229, 349, 359, 563, 569, 643, 649, 853, 859, 1063, 1069, 1283, 1289, 1459, 1469, 1679, 1681, 1849, 1859, 2063, 2069]
Source code in the ruby programming language
require 'prime'
deceptives = Enumerator.new do |y|
10.step(by: 10) do |n|
[1,3,7,9].each do |digit|
cand = n + digit
next if cand % 3 == 0 || cand.prime?
repunit = ("1"*(cand-1)).to_i
y << cand if (repunit % cand) == 0
end
end
end
p deceptives.take(25).to_a
You may also check:How to resolve the algorithm Mian-Chowla sequence step by step in the Nim programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the J programming language
You may also check:How to resolve the algorithm Date format step by step in the Perl programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the dc programming language
You may also check:How to resolve the algorithm Date format step by step in the REBOL programming language