How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Wren programming language
Table of Contents
Problem Statement
Sometimes, one may need (or want) a loop which its iterator (the index variable) is modified within the loop body in addition to the normal incrementation by the (do) loop structure index.
Demonstrate the best way to accomplish this.
Write a loop which:
Extra credit: because of the primes get rather large, use commas within the displayed primes to ease comprehension.
Show all output here.
Not all programming languages allow the modification of a loop's index. If that is the case, then use whatever method that is appropriate or idiomatic for that language. Please add a note if the loop's index isn't modifiable.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Wren programming language
Source code in the wren programming language
import "./fmt" for Fmt
var isPrime = Fn.new { |n|
if (n < 2 || !n.isInteger) return false
if (n%2 == 0) return n == 2
if (n%3 == 0) return n == 3
var d = 5
while (d*d <= n) {
if (n%d == 0) return false
d = d + 2
if (n%d == 0) return false
d = d + 4
}
return true
}
var count = 0
var i = 42
while (count < 42) {
if (isPrime.call(i)) {
count = count + 1
System.print("%(Fmt.d(2, count)): %(Fmt.dc(18, i))")
i = 2 * i - 1
}
i = i + 1
}
You may also check:How to resolve the algorithm Comments step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the RPL programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Lingo programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Swift programming language
You may also check:How to resolve the algorithm FASTA format step by step in the Forth programming language