[Year 12 SofDev] Programming Help
Victoria Farrell
FarrellV at humegrammar.vic.edu.au
Tue Apr 29 08:27:14 EST 2014
Hi,
Very new to Software Development and it's been 20 odd years since programming at Uni, I could use some help with a problem.
I recently had students involved in the Melbourne University Programming Competition and had problems with solving the following question:
A positive integer x > 1 is a prime if its only factors are 1 and x. Hence the numbers 2, 11, and 101 are primes, but 1, 9,
and 1001 are not prime*.
The input to your program is a single positive integer n. The output should be the number of primes that are _ n.
For example, if the input is 1000 then the output is 168.
* In case you're wondering, 1001 = 7 _ 11 _ 13.
Your five answers should be for
1. n = 10
2. n = 100
3. n = 100; 000
4. n = 10; 000; 000
5. n = 20; 000; 000
We decided to solve in Small Basic so as to get a handle on lops and arrays, but did not manage to solve it. I found a solution online (below) but was not able to understand how the array worked. It doesn't solve the problem exactly as it just lists the input number of primes, but it certain can calculate/select for primes.
Init:
TextWindow.WriteLine("How many Prime numbers (1-1000)? ")
N=TextWindow.ReadNumber()
If N<1 Or N>1000 Then
TextWindow.WriteLine("That's All Folks!")
TextWindow.Pause()
Program.End()
EndIf
Prime="1=2;"
Num=Prime[Array.GetItemCount(Prime)]
MainLoop:
Num=Num+1
Flag=1
For i=1 To Array.GetItemCount(Prime)
If (Math.Remainder(Num,Prime[i])=0) Then
'Not Prime
Flag=0
i=Array.GetItemCount(Prime)
EndIf
EndFor
If Flag=1 Then
Prime[Array.GetItemCount(Prime)+1]=Num
TextWindow.Write(Array.GetItemCount(Prime))
TextWindow.WriteLine(" "+Num)
if Array.GetItemCount(Prime)>=N Then
Goto Ender
EndIf
EndIf
Goto MainLoop
Ender:
'TextWindow.Write("Press any key to continue...")
TextWindow.WriteLine("---")
TextWindow.Pause()
Goto Init
Can anyone give me a brief written description, or point me to an online resource that can explain how this array functions? Can anyone identify a Small Basic function more suited to this problem?
Thanks,
Vic
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20140428/f69add16/attachment-0001.html
More information about the sofdev
mailing list