How to resolve the algorithm Radical of an integer step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Radical of an integer step by step in the Wren programming language
Table of Contents
Problem Statement
The radical of a positive integer is defined as the product of its distinct prime factors.
Although the integer 1 has no prime factors, its radical is regarded as 1 by convention.
The radical of 504 = 2³ x 3² x 7 is: 2 x 3 x 7 = 42.
- Find and show on this page the radicals of the first 50 positive integers.
- Find and show the radicals of the integers: 99999, 499999 and 999999.
- By considering their radicals, show the distribution of the first one million positive integers by numbers of distinct prime factors (hint: the maximum number of distinct factors is 7).
By (preferably) using an independent method, calculate the number of primes and the number of powers of primes less than or equal to one million and hence check that your answer in 3. above for numbers with one distinct prime is correct.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Radical of an integer step by step in the Wren programming language
Source code in the wren programming language
import "./math" for Int, Nums
import "./seq" for Lst
import "./fmt" for Fmt
var radicals = List.filled(51, 0)
radicals[1] = 1
var counts = List.filled(8, 0)
counts[1] = 1
for (i in 2..1e6) {
var factors = Lst.prune(Int.primeFactors(i))
var fc = factors.count
counts[fc] = counts[fc] + 1
if (i <= 50) radicals[i] = Nums.prod(factors)
if (i == 50) {
System.print("The radicals for the first 50 positive integers are:")
Fmt.tprint("$2d ", radicals.skip(1), 10)
System.print()
} else if (i == 99999 || i == 499999 || i == 999999) {
Fmt.print("Radical for $,7d: $,7d", i, Nums.prod(factors))
} else if (i == 1e6) {
System.print("\nBreakdown of numbers of distinct prime factors")
System.print("for positive integers from 1 to 1,000,000:")
for (i in 1..7) {
Fmt.print(" $d: $,8d", i, counts[i])
}
Fmt.print(" ---------")
Fmt.print(" $,8d", Nums.sum(counts))
Fmt.print("\nor graphically:")
Nums.barChart("", 50, Nums.toStrings(1..7), counts[1..-1])
}
}
var pcount = Int.primeCount(1e6)
var ppcount = 0
var primes1k = Int.primeSieve(1000)
for (p in primes1k) {
var p2 = p
while (true) {
p2 = p2 * p
if (p2 > 1e6) break
ppcount = ppcount + 1
}
}
Fmt.print("\nFor primes or powers (>1) thereof <= 1,000,000:")
Fmt.print(" Number of primes = $,6d", pcount)
Fmt.print(" Number of powers = $,6d", ppcount)
Fmt.print(" Add 1 for number 1 = $,6d", 1)
Fmt.print(" ------")
Fmt.print(" $,6d", pcount + ppcount + 1)
You may also check:How to resolve the algorithm McNuggets problem step by step in the Clojure programming language
You may also check:How to resolve the algorithm Count in octal step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the Swift programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the J programming language
You may also check:How to resolve the algorithm Numbers which are not the sum of distinct squares step by step in the C++ programming language