Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
(Added Lua version) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 864: | Line 864: | ||
n = 41 49752014150467 |
n = 41 49752014150467 |
||
n = 42 99504028301131</pre> |
n = 42 99504028301131</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |
||
Line 977: | Line 978: | ||
Same as Kotlin entry |
Same as Kotlin entry |
||
</pre> |
</pre> |
||
=={{header|C++}}== |
|||
<lang cpp> |
|||
#include "stdafx.h" |
|||
#include <iostream> |
|||
#include <math.h> |
|||
using namespace std; |
|||
=={{header|C sharp|C#}}== |
|||
bool isPrime(double number) |
|||
{ |
|||
for (double i = number - 1; i >= 2; i--) { |
|||
if (fmod(number, i) == 0) |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
int main() |
|||
{ |
|||
double i = 42; |
|||
int n = 0; |
|||
while (n < 42) |
|||
{ |
|||
if (isPrime(i)) |
|||
{ |
|||
n++; |
|||
cout.width(1); cout << left << "n = " << n; |
|||
//Only for Text Alignment |
|||
if (n < 10) |
|||
{ |
|||
cout.width(40); cout << right << i << endl; |
|||
} |
|||
else |
|||
{ |
|||
cout.width(39); cout << right << i << endl; |
|||
} |
|||
i += i - 1; |
|||
} |
|||
i++; |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
=={{header|C#}}== |
|||
<lang csharp> |
<lang csharp> |
||
using System; |
using System; |
||
Line 1,100: | Line 1,061: | ||
n = 42 99,504,028,301,131 |
n = 42 99,504,028,301,131 |
||
</pre> |
</pre> |
||
=={{header|C++}}== |
|||
<lang cpp> |
|||
#include "stdafx.h" |
|||
#include <iostream> |
|||
#include <math.h> |
|||
using namespace std; |
|||
bool isPrime(double number) |
|||
{ |
|||
for (double i = number - 1; i >= 2; i--) { |
|||
if (fmod(number, i) == 0) |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
int main() |
|||
{ |
|||
double i = 42; |
|||
int n = 0; |
|||
while (n < 42) |
|||
{ |
|||
if (isPrime(i)) |
|||
{ |
|||
n++; |
|||
cout.width(1); cout << left << "n = " << n; |
|||
//Only for Text Alignment |
|||
if (n < 10) |
|||
{ |
|||
cout.width(40); cout << right << i << endl; |
|||
} |
|||
else |
|||
{ |
|||
cout.width(39); cout << right << i << endl; |
|||
} |
|||
i += i - 1; |
|||
} |
|||
i++; |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 1,542: | Line 1,544: | ||
25 759155483 |
25 759155483 |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<lang freebasic>' version 18-01-2019 |
<lang freebasic>' version 18-01-2019 |
||
Line 2,670: | Line 2,673: | ||
8 5,779 |
8 5,779 |
||
9 11,579 |
9 11,579 |
||
10 23,159 |
|||
11 46,327 |
|||
12 92,657 |
|||
13 185,323 |
|||
14 370,661 |
|||
15 741,337 |
|||
16 1,482,707 |
|||
17 2,965,421 |
|||
18 5,930,887 |
|||
19 11,861,791 |
|||
20 23,723,597 |
|||
21 47,447,201 |
|||
22 94,894,427 |
|||
23 189,788,857 |
|||
24 379,577,741 |
|||
25 759,155,483 |
|||
26 1,518,310,967 |
|||
27 3,036,621,941 |
|||
28 6,073,243,889 |
|||
29 12,146,487,779 |
|||
30 24,292,975,649 |
|||
31 48,585,951,311 |
|||
32 97,171,902,629 |
|||
33 194,343,805,267 |
|||
34 388,687,610,539 |
|||
35 777,375,221,081 |
|||
36 1,554,750,442,183 |
|||
37 3,109,500,884,389 |
|||
38 6,219,001,768,781 |
|||
39 12,438,003,537,571 |
|||
40 24,876,007,075,181 |
|||
41 49,752,014,150,467 |
|||
42 99,504,028,301,131</pre> |
|||
=={{header|Perl 6}}== |
|||
Hmm. |
|||
<blockquote>Demonstrate the best way to accomplish this. </blockquote> |
|||
The ''best'' way is probably to not use an explicit loop. Just calculate the sequence directly. |
|||
<lang perl6># the actual sequence logic |
|||
my @seq = grep *.is-prime, (42, { .is-prime ?? $_+<1 !! $_+1 } … *); |
|||
# display code |
|||
say (1+$_).fmt("%-4s"), @seq[$_].flip.comb(3).join(',').flip.fmt("%20s") for ^42;</lang> |
|||
{{out}} |
|||
<pre>1 43 |
|||
2 89 |
|||
3 179 |
|||
4 359 |
|||
5 719 |
|||
6 1,439 |
|||
7 2,879 |
|||
8 5,779 |
|||
9 11,579 |
|||
10 23,159 |
10 23,159 |
||
11 46,327 |
11 46,327 |
||
Line 2,949: | Line 2,898: | ||
42: 99,504,028,301,131 |
42: 99,504,028,301,131 |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Hmm. |
|||
<blockquote>Demonstrate the best way to accomplish this. </blockquote> |
|||
The ''best'' way is probably to not use an explicit loop. Just calculate the sequence directly. |
|||
<lang perl6># the actual sequence logic |
|||
my @seq = grep *.is-prime, (42, { .is-prime ?? $_+<1 !! $_+1 } … *); |
|||
# display code |
|||
say (1+$_).fmt("%-4s"), @seq[$_].flip.comb(3).join(',').flip.fmt("%20s") for ^42;</lang> |
|||
{{out}} |
|||
<pre>1 43 |
|||
2 89 |
|||
3 179 |
|||
4 359 |
|||
5 719 |
|||
6 1,439 |
|||
7 2,879 |
|||
8 5,779 |
|||
9 11,579 |
|||
10 23,159 |
|||
11 46,327 |
|||
12 92,657 |
|||
13 185,323 |
|||
14 370,661 |
|||
15 741,337 |
|||
16 1,482,707 |
|||
17 2,965,421 |
|||
18 5,930,887 |
|||
19 11,861,791 |
|||
20 23,723,597 |
|||
21 47,447,201 |
|||
22 94,894,427 |
|||
23 189,788,857 |
|||
24 379,577,741 |
|||
25 759,155,483 |
|||
26 1,518,310,967 |
|||
27 3,036,621,941 |
|||
28 6,073,243,889 |
|||
29 12,146,487,779 |
|||
30 24,292,975,649 |
|||
31 48,585,951,311 |
|||
32 97,171,902,629 |
|||
33 194,343,805,267 |
|||
34 388,687,610,539 |
|||
35 777,375,221,081 |
|||
36 1,554,750,442,183 |
|||
37 3,109,500,884,389 |
|||
38 6,219,001,768,781 |
|||
39 12,438,003,537,571 |
|||
40 24,876,007,075,181 |
|||
41 49,752,014,150,467 |
|||
42 99,504,028,301,131</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,421: | Line 3,425: | ||
i=41 : 49,752,014,150,467 |
i=41 : 49,752,014,150,467 |
||
i=42 : 99,504,028,301,131</pre> |
i=42 : 99,504,028,301,131</pre> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Visual Basic .Net allows to modify the index inside the loop. |
Visual Basic .Net allows to modify the index inside the loop. |
||
Line 3,510: | Line 3,515: | ||
i=41 : 49,752,014,150,467 |
i=41 : 49,752,014,150,467 |
||
i=42 : 99,504,028,301,131</pre> |
i=42 : 99,504,028,301,131</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Uses libGMP (GNU MP Bignum Library) for easy prime detection |
Uses libGMP (GNU MP Bignum Library) for easy prime detection |