dMZX Forums: Sieve of Eratosthenes - dMZX Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Sieve of Eratosthenes

#1 User is offline   nooodl 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 735
  • Joined: 28-October 06
  • Gender:Not Telling
  • Location:Belgium

Posted 30 October 2007 - 06:48 PM

I've been wondering if the Sieve of Eratosthenes would be possible to program into Robotic...
I've been trying to, but I can't do it. But maybe someone else here can...?

First person to do it gets one million internets.
0

#2 User is offline   Wervyn 

  • I can see you
  • Group: DigiStaff
  • Posts: 1,855
  • Joined: 24-December 00
  • Gender:Male
  • Location:Caras Galadhon

Posted 30 October 2007 - 07:55 PM

: "sieve"
set "local1" to "LIMIT" (set this number yourself somewhere)
set "local2" to "sqrt&local1&"
goto "#genlist"
set "local3" to 2
: "L"
if "isprime&local3&" = 1 then "#sievelist"
inc "local3" by 1
if "local3" <= "local2" then "L"
goto "#printlist"
end

: "#genlist"
loop start
set "isprime&loopcount&" to 1
loop for "local1"
set "isprime0" to 0
set "isprime1" to 0
goto "#return"

: "#sievelist"
set "local4" to "('local3'*2)"
: "SL"
set "isprime&local4&" to 0
inc "local4" by "local3"
if "local4" <= "local1" then "SL"
goto "#return"

: "#printlist"
set "$list" to ""
loop start
if "isprime&loopcount&" = 0 then "PS"
inc "$list" by "&loopcount& "
: "PS"
loop for "local1"

set "local3" to 0
if "$list.length" <= 80 then "EN"
: "PL"
goto "#findspace"
write overlay c0f "&$list#('local4'+1)&" at 0 "local3"
set "$list" to "$list+('local4'+1)"
inc "local3" by 1
if "$list.length" > 80 then "PL"
: "EN"
write overlay c0f "&$list&" at 0 "local3"
goto "#return"

: "#findspace"
set "local4" to 79
: "F"
if "$list.&local4&" = 32 then "#return"
dec "local4" by 1
if "local4" > 0 then "F"
set "local4" to 79
goto "#return"


Written without testing. So it may have typos, or bugs. But something like this should work.

EDIT: And there's one of them, in the string printing code.
EDIT 2: Added some more code to help print out the primes in a more readable form than the message row. This will dump them on the overlay and does some simple wrapping. Try a number like 10000, should take less than a second to run with commands set to 32767.

"Robotic is not a GCS!"
To lie is to change the truth.
..Ignorance is to be unaware of the truth.
....Incompetence is to be unable to grasp the truth.
......And escape is to run away from the truth.
It is useless to run, since the truth is right next to you.

-Wervyn
0

#3 User is offline   nooodl 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 735
  • Joined: 28-October 06
  • Gender:Not Telling
  • Location:Belgium

Posted 30 October 2007 - 08:32 PM

Topic over Wervyn wins

Heh. Thanks. :)
0

#4 User is offline   T-Bone 

  • Wastelander
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,487
  • Joined: 16-August 02
  • Gender:Male
  • Location:Canada

Posted 30 October 2007 - 09:09 PM

Quote

First person to do it gets one million internets.


Now will that be one million KB of bandwidth?
Youtube - teabone3 - Twitter - teabone3 - Twitch - teabone3
0

#5 User is offline   nooodl 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 735
  • Joined: 28-October 06
  • Gender:Not Telling
  • Location:Belgium

Posted 31 October 2007 - 09:17 AM

1000000 TB :D
0

#6 User is offline   RoSS 

  • RIP Fred Friedberg 2004
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 3,283
  • Joined: 22-October 00
  • Gender:Male
  • Location:Right in front of behind you.

Post icon  Posted 31 October 2007 - 12:30 PM

Ooh, ooh, now do sieve of atkin!
-RoSS
<img src="http://ross.box43.net/sig.php/sig.png" border="0" class="linked-sig-image" />
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users