Jump to content

Loops/Increment loop index within loop body: Difference between revisions

no edit summary
(add task to ARM64 assembly Raspberry Pi)
No edit summary
Line 1,974:
Same as Kotlin entry
</pre>
=={{header|Haskell}}==
No index mutations or loops. Recursion is used.
<lang haskell>
import Data.List
 
isPrime :: Int -> Bool
isPrime n
| n <= 3 = n > 1
| n `mod` 2 == 0 || n `mod` 3 == 0 = False
| otherwise = isPrime' 5 n
where isPrime' d n
| x <= n = isPrime'' d n
| otherwise = True
where x = d * d
isPrime'' d n
| n `mod` d == 0 = False
| n `mod` (d + 2) == 0 = False
| otherwise = isPrime' (d + 6) n
 
showPrime :: Int -> Int -> [(Int, Int)]
showPrime i n
| isPrime i = (n, i) : showPrime (i+i) (n+1)
| otherwise = showPrime (i+1) n
 
digitGroup :: Int -> String
digitGroup = intercalate "," . reverse . map show . digits
where digits = unfoldr (\n -> if n == 0 then Nothing
else Just (n `mod` 1000, n `div` 1000))
 
display :: (Int, Int) -> String
display (i, p) = show i ++ " " ++ digitGroup p
 
main = mapM_ putStrLn $ map display $ take 42 $ showPrime 42 1
</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,36,621,941
28 6,73,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,81
36 1,554,750,442,183
37 3,109,500,884,389
38 6,219,1,768,781
39 12,438,3,537,571
40 24,876,7,75,181
41 49,752,14,150,467
42 99,504,28,301,131
</pre>
=={{header|Haxe}}==
Haxe's for-loop does allow the index to be modified in the body of the loop, so a while-loop is used instead.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.