Prime Finder

Blitz3D Forums/Blitz3D Programming/Prime Finder

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Is there a function anyone has made that shows you prime numbers in some way or tells you whether a number is prime or not? I was just wondering because I just made one that works perfectly, but I'm only going to put it in the Code Archives if there's not already one.

The one that I saw in the archives is really long
http://blitzbasic.com/codearcs/codearcs.php?code=1888
but the one I made is way shorter and it works.

Hehe, it's been 1 hour so i'll take the "no replies" as a no.

Graphics 500,700,1,2


For n = 0 To 21
	Text 0,n*30,"n = " + n
	If Prime(n) = 1
		Text 20,(n*30)+10,"Is n("+n+") a prime number? Yes"
			Else
				Text 20,(n*30)+10,"Is n("+n+") a prime number? No"
	EndIf
Next

WaitKey()

Function Prime(prime%)
	Local Not_Primes% ; If the givin number is prime Ex: 5
					 ; then it will only return 2 Whole Numbers when it's divided by all of the numbers before it : 1 to 5
					
					 ; Everytime it returns a Whole Number, then Not_Primes = Not_Primes + 1
					 ; A prime number should only have 2 Whole Numbers and the rest are ones with a decimal point.
					
					 ; If Not_Primes only = 2 at the end, then the number is a prime number
					
		For divide = 1 To prime
			If Float(prime)/divide = Int(Float(prime)/divide)
				Not_Primes = Not_Primes + 1
			EndIf
		Next
		
		If Not_Primes = 2
			Return True
				Else
					Return False
		EndIf
End Function