[Year 12 SofDev] Programming Help

Victoria Farrell FarrellV at humegrammar.vic.edu.au
Wed Apr 30 07:43:47 EST 2014


Thank you SO MUCH Peter, this has helped a lot!!!

Vic

From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Coventry, Peter J
Sent: Tuesday, 29 April 2014 7:06 PM
To: Year 12 Software Development Teachers' Mailing List
Subject: Re: [Year 12 SofDev] Programming Help

The program you have is slightly different in its intentions - it asks for a number n and then generates the first n prime numbers.

However it could be adapted fairly easily by modifying the exit condition for the loop. (i.e. the program would ask for the last number and then check if Num=n to exit MainLoop. The Ender section would print Array.GetItemCount(Prime)

I've added some comments to the program to hopefully explain how it works.

Disclaimer: I don't teach SD, and don't really know Small Basic - I did a fair bit of programming in another life and still enjoy the occasional dabble.

Cheers,
Peter

From: sofdev-bounces at edulists.com.au<mailto:sofdev-bounces at edulists.com.au> [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Victoria Farrell
Sent: Tuesday, 29 April 2014 8:27 AM
To: Year 12 Software Development Teachers' Mailing List
Subject: [Year 12 SofDev] Programming Help

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;"  ' Prime is an array to hold the prime numbers we find. Start by creating a single element array with value 2 - the first prime number
Num=Prime[Array.GetItemCount(Prime)] ' Num is the current number we are checking to see if it is prime. This gets the value of the last element in Prime. Could have also said Num=2


MainLoop:
Num=Num+1 ' looking at the next number to see if it is prime

  Flag=1 ' Flag is 1 if Num is prime

  For i=1 To Array.GetItemCount(Prime)  ' Loop through each of the prime numbers in the array so far,
    If (Math.Remainder(Num,Prime[i])=0) Then  ' check if the prime number is a factor of Num
      'Not Prime
      Flag=0 ' resets Flag
      i=Array.GetItemCount(Prime) ' sets i to the last value so it exits this loop
    EndIf
  EndFor
  If Flag=1 Then 'if we get to here with Flag=1 then the number has no factors besides itself - so it is prime
    Prime[Array.GetItemCount(Prime)+1]=Num  'add this number to the end of the Prime array
    TextWindow.Write(Array.GetItemCount(Prime))  'print something out
    TextWindow.WriteLine("   "+Num)
    if Array.GetItemCount(Prime)>=N Then  'If we have found the number of primes requested, then exit the MainLoop
      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

Important - This email and any attachments may be confidential. If received in error, please contact us and delete all copies. Before opening or using attachments check them for viruses and defects. Regardless of any loss, damage or consequence, whether caused by the negligence of the sender or not, resulting directly or indirectly from the use of any attached files our liability is limited to resupplying any affected attachments. Any representations or opinions expressed are those of the individual sender, and not necessarily those of the Department of Education and Early Childhood Development.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20140429/140c0e00/attachment-0001.html 


More information about the sofdev mailing list