Sieve for PocketC & NSBasicCE


This source code is freeware. However, please do not distribute modified versions. Thanks.

This code should compile and run using PocketC .

This code should run using NSBasic.

If you download and run this program, please send me your timings and I'll post them here for comparison purposes.

The Source Code

NOTE: It appears that on WindowsCE the more memory you can give programs the faster this runs. The above timings are the fastest noted.

POCKET C

//----------------------------------------------
// Sieve
//
//   Serg Koren
//   VisualNewt Software
//   http://www.VisualNewt.com/
//   Serg@VisualNewt.com
//
//  Ver 2.0
//    Total rewrite to work under PocketC b1.30
//
//-------------
//
/* Global defines */
#define WS_CHILD 0x40000000
#define WS_POPUP 0x80000000
#define WS_VISIBLE  0x10000000
#define SIZE  8193
#define TRUE  1
#define FALSE 0
/* Interface controls - menu */
#define	MF_SEPARATOR	0x0800
#define	MF_ENABLED		0x0000
#define	MF_STRING		0x0000
/* Event types */
#define PM_BUTTONUP 5
#define PM_COMMAND 8
/* Our button */
#define SIEVE 100
/* our sieve array */
#define SIZE 8193
int x[SIZE];
//----------------------------------------------
// ShowResults - update our labels
//----------------------------------------------
//
ShowResults(int start, int end, int elapsed)
{
 text(20,56, "Start:  " + start);
	text(20,83,"End:    " + end);
	text(20,108,"Elapsed:" + elapsed);
	text(20,140,"");
}
//----------------------------------------------
// DoIt
//     The main sieve logic.  Pass in the max.
// number of primes to search to.
//----------------------------------------------
//
DoIt(int n)
{
	int i, j, k, p, iter, start;
	text(20,140,"Working...");
	start = ticks();
	for (iter = 1; iter <= 10; iter++)
	{
		x[1] = 0;
		for (i = 2; i <= n; i++)
			x[i] = 1;
		p = 2;
		while (p * p <= n)
		{
			j = p;
			while (j <= n)
			{
				x[j] = 0;
				j = j + p;
			}
			do
			{
				p++;
			} while (x[p] != 1);
		}
	}
	// output details
	j = ticks();
	k =j - start;
	ShowResults(start,j,k);
	// display an alert with results
	alert("Done!  In:  " + k + " ticks.");
}
//----------------------------------------------
// draw_form
//	Setup our GUI
//----------------------------------------------
//
draw_form()
{
	menu_on();
	menuins(0,0,MF_SEPARATOR,200,"");
	menuins(0,0,MF_ENABLED|MF_STRING,201,"PalmOS (5120)");
	menuins(0,0,MF_ENABLED|MF_STRING,202,"Full (8192)");
	text(20,56,"Start:");
	text(20,83,"End:");
	text(20,108,"Elapsed:");
	text(20,140,"");
	createctrl("BUTTON","Sieve",WS_CHILD|WS_VISIBLE,0,82,150,75,25,100);
}
//----------------------------------------------
// SetupAbout
//	Build our about box
//----------------------------------------------
//
SetupAbout()
{
	about("SieveCE - PocketC  by Serg Koren.  VisualNewt Software  http://www.VisualNewt.com/   Ver. 2.0");
}
//----------------------------------------------
// MAIN
//   our main routine
//----------------------------------------------
//
main()
{
	int e,g,m,k;
	int NotDone;
	NotDone = TRUE;
	draw_form();
	SetupAbout();
	while (NotDone)
      {
      	e=event(0);
      	k=key();
		if (e==PM_BUTTONUP)
		{
			g=guiid();
			if (g==SIEVE)
				DoIt(5120);
			m=menu();
		}
		if (e==PM_COMMAND)
		{
			g=guiid();
			if (g == 201)
				DoIt(5120);		// PalmOS
			else if (g == 202)
				DoIt(8192);		// Full
			m=menu();
		}
	}
}
//-------------------END------------------------
              

NSBASIC CE


'----------------------------------------------------
'  Sieve
'
'      Serg Koren
'      VisualNewt Software
'      http://www.VisualNewt.com/
'      Serg@VisualNewt.com
'
'  03/19/99
'       Ver 1.0
'
'------------------------------------------------------
'
OPTION EXPLICIT
DIM Version    ' version string
Version = "Ver. 1.0"
'------------------------------------------------------
' DoIt
'
'  The actual sieve logic.
'------------------------------------------------------
'
SUB DoIt(size)
  Feedback.Caption = "Working..."
  UPDATESCREEN ' allow menus to refresh, etc.
  DIM  i, j, k, p, iter, howlong
  DIM x(8193)
  howlong = NOW
  FOR iter = 1 To 10
    x(1) = 0
    FOR i = 2 TO size
      x(i) = 1
    NEXT
    p = 2
    WHILE p^2 <= size
      j = p
      WHILE j <= size
        x(j) = 0
        j = j + p
      WEND
      DO
        p = p + 1
      LOOP WHILE x(p) <> 1
    WEND
  NEXT
  j = NOW
  ' display results
  Feedback.Caption = ""
  Start.Caption = "Start:  " & howlong
  EndL.Caption = "End:   " &  j
  Elap.caption =  "Elap.:  " &  DATEDIFF("s",howlong,j) & " sec."
END SUB
'------------------------------------------------------
' About menu item
'------------------------------------------------------
'
SUB About_click
  DIM T
  DIM CRLF
  CRLF = CHR(13) & CHR(10)
  T = "SieveCE - NSBasic" & CRLF & "by Serg Koren" & CRLF & CRLF & "VisualNewt Software"
  T = T & CRLF & CRLF & "http://www.VisualNewt.com/" & CRLF & CRLF & Version
  MSGBOX T,vbOKOnly,"About Sieve"
END SUB
'------------------------------------------------------
'  Exit menu item
'------------------------------------------------------
'
SUB Exit_click
  BYE
END SUB
'------------------------------------------------------
' PalmOS sieve request
'------------------------------------------------------
'
SUB PalmOS_click
  DoIt(5120)
END SUB
'------------------------------------------------------
' Full sieve request
'------------------------------------------------------
'
SUB Full_click
  DoIt(8192)
END SUB
'------------------------------------------------------
'  Sieve button clicked
'------------------------------------------------------
'
SUB Sieve_click
   DoIt(5120)
END SUB
'------------------------------------------------------
' SetupMenus
'------------------------------------------------------
'
SUB SetupMenus
  DIM titlebar
  titlebar = ARRAY("&File","&Command")
  SETMENU "titlebar",titlebar
  DIM fileMenu
  fileMenu=array("About||About","-","E&xit||Exit")
  SETMENU "&File",fileMenu
  DIM commandMenu
  commandMenu=array("Full (8192)||Full","PalmOS (5120)||PalmOS")
  SETMENU "&Command",commandMenu
END SUB
'------------------------------------------------------
' SetupLabels
'   for user feedback
'------------------------------------------------------
'
SUB SetupLabels
  ADDOBJECT "Label", "Start", 10, 10, 200,20
  Start.Caption = "Start:"
  Start.BackColor = Output.BackColor
  ADDOBJECT "Label","EndL",10, 30, 200, 20
  EndL.Caption = "End:"
  EndL.BackColor = Output.BackColor
  ADDOBJECT "Label","Elap", 10, 50, 200,20
  Elap.Caption = "Elap.:"
  Elap.BackColor = Output.BackColor
  ADDOBJECT "Label","Feedback", 10,70,200,20
  Feedback.Caption = ""
  Feedback.BackColor = Output.BackColor
END SUB
'------------------------------------------------------
' SetupButton
'------------------------------------------------------
'
SUB SetupButton
  ADDOBJECT "CommandButton","Sieve", 50, 130, 50,20
END SUB
'------------------------------------------------------
' SetupGUI
'  Build our user interface
'------------------------------------------------------
'
SUB SetupGUI
  SetupMenus
  SetupLabels
  SetupButton
END SUB
'------------------------------------------------------
'  MAIN
'------------------------------------------------------
'
SUB Main
  SetupGUI  ' and wait for events
END SUB
'------------------------------------------------------
'------------------------------------------------------
' run the program
Main
'------------------------------------------------------
'------------------------------------------------------
'----------------------END-----------------------------
              
©2007 Serg Koren & VisualNewt Software.  All rights reserved.