Created
July 11, 2021 08:00
-
-
Save andrew-wilkes/0c582c9a8d6d1553daef2027f8444a06 to your computer and use it in GitHub Desktop.
Prime Numbers with GDScript
This file contains 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
extends Node2D | |
export var nmax = 300 | |
func _ready(): | |
# Find prime numbers | |
sieve_of_eratosthenes(nmax) | |
func sieve_of_eratosthenes(n: int): | |
var nums = range(n + 1) | |
for i in range(2, sqrt(n) + 1): | |
var j = i * i | |
while j <= n: | |
nums[j] = 0 | |
j += i | |
var primes: Array | |
for p in nums: | |
if p > 1: | |
primes.append(p) | |
print(primes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment