Microsoft Small Basic

Problem 10 of Project Euler

Modified: 2008/12/26 08:51 by 24.218.11.239 - Uncategorized
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

GC = 3
Array.SetValue("Primes",1,2)
Array.SetValue("Primes",2,3)
Array.SetValue("Primes",3,5)

For i = 7 To 2000000 Step 2
  'If Math.Remainder(i,10011) = 0 Then
  '  TextWindow.WriteLine(i)
  'EndIf
  If Math.Remainder(i,5) = 0 Then
    Goto Skip
  EndIf
  CheckPrime()
  If isPrime = "True" Then
    'TextWindow.WriteLine(i + " Prime found")
    GC = GC + 1
    Array.SetValue("Primes",GC,i)
    sum = sum + i
  EndIf
  Skip:
EndFor

Sub CheckPrime
  isPrime = "True"
  For j = 1 To GC
    If( Array.GetValue("Primes",j) * Array.GetValue("Primes",j) > i ) Then
      Goto EndLoop
    EndIf
    If Math.Remainder(i, Array.GetValue("Primes",j)) = 0 Then
      isPrime = "False"
      Goto EndLoop
    EndIf
  EndFor
  EndLoop:
EndSub

TextWindow.WriteLine("All primes under 2,000,000 added together is " + (sum+10) )

ScrewTurn Wiki version 2.0.35. Some of the icons created by FamFamFam.