How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Haxe 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 Haxe 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 Haxe programming language
Source code in the haxe programming language
using StringTools;
import haxe.Int64;
class PrimeNumberLoops {
private static var limit = 42;
static function isPrime(i:Int64):Bool {
if (i == 2 || i == 3) {
return true;
} else if (i % 2 == 0 || i % 3 ==0) {
return false;
}
var idx:haxe.Int64 = 5;
while (idx * idx <= i) {
if (i % idx == 0) return false;
idx += 2;
if (i % idx == 0) return false;
idx += 4;
}
return true;
}
static function main() {
var i:Int64 = 42;
var n:Int64 = 0;
while (n < limit) {
if (isPrime(i)) {
n++;
Sys.println('n ${Int64.toStr(n).lpad(' ', 2)} ' +
'= ${Int64.toStr(i).lpad(' ', 19)}');
i += i;
continue;
}
i++;
}
}
}
You may also check:How to resolve the algorithm Twin primes step by step in the Python programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the 11l programming language
You may also check:How to resolve the algorithm Stirling numbers of the second kind step by step in the C++ programming language