How to resolve the algorithm Blum integer step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Blum integer step by step in the Wren programming language

Table of Contents

Problem Statement

A positive integer n is a Blum integer if n = p x q is a semi-prime for which p and q are distinct primes congruent to 3 mod 4. In other words, p and q must be of the form 4t + 3 where t is some non-negative integer.

21 is a Blum integer because it has two prime factors: 3 (= 4 x 0 + 3) and 7 (= 4 x 1 + 3). Find and show on this page the first 50 Blum integers. Also show the 26,828th.

Find and show the 100,000th, 200,000th, 300,000th and 400,000th Blum integers. For the first 400,000 Blum integers, show the percentage distribution by final decimal digit (to 3 decimal places). Clearly, such integers can only end in 1, 3, 7 or 9.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Blum integer step by step in the Wren programming language

Source code in the wren programming language

import "./math" for Int
import "./fmt" for Fmt

var inc = [4, 2, 4, 2, 4, 6, 2, 6]

// Assumes n is odd.
var firstPrimeFactor = Fn.new { |n|
    if (n == 1) return 1
    if (n%3 == 0) return 3
    if (n%5 == 0) return 5
    var k = 7
    var i = 0
    while (k * k <= n) {
        if (n%k == 0)  {
            return k
        } else {
            k = k + inc[i]
            i = (i + 1) % 8
        }
    }
    return n
}

var blum = List.filled(50, 0)
var bc = 0
var counts = { 1: 0, 3: 0, 7: 0, 9: 0 }
var i = 1
while (true) {
    var p = firstPrimeFactor.call(i)
    if (p % 4 == 3) {
        var q = i / p
        if (q != p && q % 4 == 3 && Int.isPrime(q)) {
            if (bc < 50) blum[bc] = i
            counts[i % 10] = counts[i % 10] + 1
            bc = bc + 1
            if (bc == 50) {
                System.print("First 50 Blum integers:")
                Fmt.tprint("$3d ", blum, 10)
                System.print()
            } else if (bc == 26828 || bc % 1e5 == 0) {
                Fmt.print("The $,9r Blum integer is: $,9d", bc, i)
                if (bc == 400000) {
                    System.print("\n\% distribution of the first 400,000 Blum integers:")
                    for (i in [1, 3, 7, 9]) {
                        Fmt.print("  $6.3f\% end in $d", counts[i]/4000, i)
                    }
                    return
                }
            }
        }
    }
    i = (i % 5 == 3) ? i + 4 : i + 2
}


  

You may also check:How to resolve the algorithm Variables step by step in the Ruby programming language
You may also check:How to resolve the algorithm Abstract type step by step in the Go programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Quackery programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Swift programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the Arturo programming language