January 24, 2021, 12:04:56 PM

### Author Topic: [bmx] Twin Primes by daaan [ 1+ years ago ]  (Read 890 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] Twin Primes by daaan [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Twin Primes
Author : daaan
Posted : 1+ years ago

Description : I had a assembly programming assignment that was to print out the first 100 twin primes. So I made this mock up program to figure how a easy method for calculating twin primes. It does in fact work, but now I need to translate it into assembly

Code :
Code: BlitzMax
1. Rem
2. **********************************************************
3. twin primes by daniel wooden
4. **********************************************************
5. End Rem
6.
7. Local count:Int = 1
8. Local curnum:Int = 3
9.
10. Local a:Int = 0
11. Local b:Int = 0
12.
13. While count < 101 ' <-- twin primes upto the 100th place.
14.
15.         If IsPrime( curnum ) And IsPrime( curnum+2 ) Then
16.                 a = curnum
17.                 b = curnum+2
18.                 Print "Twin Prime Set: " + count + " (" + a + "," + b + ")"
19.                 count :+ 1
20.         End If
21.
22.         curnum :+ 1
23.
24. Wend
25. End
26.
27. Function IsPrime:Int( Num:Int )
28.
29.         Local Prime:Int = True
30.
31.         For i = 2 To Num/2
32.                 If (Num Mod i) = 0 Then
33.                         Prime = False
34.                 End If
35.         Next
36.
37.         Return Prime
38.
39. End Function

rdodson41(Posted 1+ years ago)

Your calculating the even numbers too, try curnum :+ 2, should go twice as fast.

Azathoth(Posted 1+ years ago)

To make IsPrime faster you should use 'Exit' after 'Prime = False' because you're still in the 'For-Next' loop when using large numbers.

`;**********************************************************;twin primes by daniel wooden;converted by shadowturtle;**********************************************************Local count = 1Local curnum = 3Local a = 0Local b = 0While count < 101 If IsPrime( curnum ) And IsPrime( curnum+2 ) Then a = curnum b = curnum+2 Print "Twin Prime Set: " + count + " (" + a + "," + b + ")" count = count + 1 End If curnum = curnum + 1 WendWaitKeyEndFunction IsPrime( Num ) Local Prime = True For i = 2 To Num/2 If (Num Mod i) = 0 Then Prime = False End If Next Return Prime End Function`