How to resolve the algorithm Semiprime step by step in the Crystal programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Semiprime step by step in the Crystal programming language

Table of Contents

Problem Statement

Semiprime numbers are natural numbers that are products of exactly two (possibly equal) prime numbers.

Semiprimes   are also known as:

(This particular number was chosen as the length of the Arecibo message).

Write a function determining whether a given number is semiprime.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Semiprime step by step in the Crystal programming language

Source code in the crystal programming language

def semiprime(n)
  nf = 0
  (2..n).each do |i|
    while n % i == 0
      return false if nf == 2
      nf += 1
      n  /= i
    end
  end
  nf == 2
end

(1675..1681).each { |n| puts "#{n} -> #{semiprime(n)}" }


def semiprime(n)
  `factor #{n}`.split(' ').size == 3
end
n = 0xffffffffffffffff_u64 # 2**64 - 1 = 18446744073709551615
(n-50..n).each { |n| puts "#{n} -> #{semiprime(n)}" }


  

You may also check:How to resolve the algorithm Floyd's triangle step by step in the VBScript programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Perl programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Nim programming language
You may also check:How to resolve the algorithm Delegates step by step in the Pascal programming language