Created
September 28, 2024 17:20
-
-
Save menjaraz/fcd125b1ebf03ade6550717dd6d0ef78 to your computer and use it in GitHub Desktop.
Prime Generator in Dlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.range; | |
import std.algorithm; | |
auto primeGenerator() { | |
int num = 2; | |
return generate(() { | |
while (true) { | |
if (isPrime(num)) { | |
return num++; | |
} | |
num++; | |
} | |
}); | |
} | |
bool isPrime(int num) { | |
if (num <= 1) return false; | |
for (int i = 2; i * i <= num; i++) { | |
if (num % i == 0) return false; | |
} | |
return true; | |
} | |
void main() { | |
const LIMIT = 100; | |
writeln(primeGenerator.until!(n => (n >= LIMIT))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment