How to resolve the algorithm Smarandache-Wellin primes step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smarandache-Wellin primes step by step in the Ruby programming language

Table of Contents

Problem Statement

A Smarandache-Wellin number (S-W number for short) is an integer that in a given base is the concatenation of the first n prime numbers written in that base. A base of 10 will be assumed for this task. A Derived S-W number (not an 'official' term) is an integer formed from a S-W number by working out the number of times each of the digits 0 to 9 occurs in that number, concatenating those frequencies in the same order (i.e. frequency of '0' first, frequency of '1' second etc) and removing any leading zeros. '23571113' is the sixth S-W number formed by concatenating the first 6 primes: 2, 3, 5, 7, 11 and 13. The corresponding Derived S-W number is '312010100' because '1' occurs 3 times, '3' occurs twice and '2', '5' and '7' all occur once. Find and show the index in the sequence (starting from 1), the total number of digits and the last prime used to form the fourth, fifth, sixth, seventh and (optionally) the eighth S-W numbers which are prime or probably prime with reasonable certainty. It is unknown whether there are any more but, if you fancy searching for one, good luck! You can start from an index of 22,077. Also find and show up to the first twenty Derived S-W numbers which are prime and their index in the sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smarandache-Wellin primes step by step in the Ruby programming language

This Ruby code explores Smarandache-Wellin primes, which are primes that are formed by concatenating the digits of their index in the sequence of prime numbers. It also examines derived Smarandache-Wellin primes, which are formed by applying a specific operation to these primes.

A step-by-step breakdown of the code:

  1. Loading Libraries:

    • require 'prime': Imports the standard Ruby library for prime number handling.
    • require 'openssl': Includes the OpenSSL library for its more efficient prime? method.
  2. Struct Definition:

    • Smarandache_Wellins is a custom data structure (Struct) with members:
      • index: Index of the prime in the sequence.
      • last_prime: The last prime used in the concatenation.
      • sw: The Smarandache-Wellin prime (index digits concatenated).
      • derived: The derived Smarandache-Wellin prime (obtained by tallying the digits of sw).
  3. Helper Methods:

    • derived(n): Calculates the derived Smarandache-Wellin prime for a given number n by counting the occurrences of each digit in n and joining the counts as a string, then converting it to an integer.
    • prime?(n): Checks if a given number n is prime using the prime? method from OpenSSL.
  4. Enumerator for Smarandache-Wellin Primes (smarandache_wellinses):

    • It generates Smarandache_Wellins objects for each prime number, concatenating the digits of each index to form the Smarandache-Wellin prime.
  5. Enumerator for Smarandache-Wellin Prime Primes (smarandache_wellins_primes):

    • It filters the smarandache_wellinses enumerator to only include those Smarandache-Wellin primes where the concatenated digits also form a prime number.
  6. Enumerator for Derived Smarandache-Wellin Prime Primes (sw_derived_primes):

    • It filters the smarandache_wellinses enumerator to only include those Smarandache-Wellin primes where the derived prime (obtained by counting digits) is also a prime number.
  7. Printing Results:

    • The code prints the first n Smarandache-Wellin primes and their derived primes.
    • It also prints detailed information about the first n Smarandache-Wellin primes, including their index, number of digits, and the last prime used in their construction.

This code provides a thorough exploration of Smarandache-Wellin primes and their derived counterparts, showcasing Ruby's powerful features for enumerating and evaluating prime numbers.

Source code in the ruby programming language

require 'prime'
require 'openssl' # for it's faster 'prime?' method

Smarandache_Wellins = Struct.new(:index, :last_prime, :sw, :derived)

def derived(n)
  counter = Array.new(10){|i| [i, 0]}.to_h # counter is a Hash
  n.digits.tally(counter).values.join.to_i
end

def prime?(n) = OpenSSL::BN.new(n).prime?

ord = {1 => "1st", 2 => "2nd", 3 => "3rd"}
ord.default_proc = proc {|_h, k| k.to_s + "th"} # _ in _h means "not used"

smarandache_wellinses = Enumerator.new do |y|
  str = ""
  Prime.each.with_index(1) do |pr, i|
    str += pr.to_s
    sw = str.to_i
    y << Smarandache_Wellins.new(i, pr, sw, derived(sw))
  end
end

smarandache_wellins_primes = Enumerator.new do |y|
  smarandache_wellinses.rewind
  loop do
    s_w = smarandache_wellinses.next
    y << s_w if prime?(s_w.sw)
  end
end

sw_derived_primes = Enumerator.new do |y|
  smarandache_wellinses.rewind
  loop do
    sw  = smarandache_wellinses.next
    y << sw if prime?(sw.derived)
  end
end

n = 3
puts "The first #{n} Smarandache-Wellin primes are:"
puts smarandache_wellins_primes.first(n).map(&:sw)
puts "\nThe first #{n} Smarandache-Wellin derived primes are:"
puts sw_derived_primes.first(n).map(&:derived)

n = 7
puts "\nThe first #{n} Smarandache-Wellin primes are:"
smarandache_wellins_primes.first(n).each.with_index(1) do |swp, i|
  puts "%s: index %4d,  digits %4d,  last prime %5d " % [ord[i], swp.index, swp.sw.digits.size, swp.last_prime]
end

n = 20
puts "\nThe first #{n} Derived Smarandache-Wellin primes are:"
sw_derived_primes.first(n).each.with_index(1) do |swdp, i| 
  puts "%4s: index %4d, prime %s" % [ord[i], swdp.index, swdp.derived]
end


  

You may also check:How to resolve the algorithm Pascal's triangle step by step in the GW-BASIC programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Arrays step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the E programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the VBScript programming language