Skip to content

Commit

Permalink
fix: Foldername changed
Browse files Browse the repository at this point in the history
  • Loading branch information
GroophyLifefor committed Apr 13, 2024
1 parent 22e777e commit 51b1221
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 0 deletions.
42 changes: 42 additions & 0 deletions exercises/practice/sieve/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Instructions

Your task is to create a program that implements the Sieve of Eratosthenes algorithm to find all prime numbers less than or equal to a given number.

A prime number is a number larger than 1 that is only divisible by 1 and itself.
For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
By contrast, 6 is _not_ a prime number as it not only divisible by 1 and itself, but also by 2 and 3.

To use the Sieve of Eratosthenes, you first create a list of all the numbers between 2 and your given number.
Then you repeat the following steps:

1. Find the next unmarked number in your list (skipping over marked numbers).
This is a prime number.
2. Mark all the multiples of that prime number as **not** prime.

You keep repeating these steps until you've gone through every number in your list.
At the end, all the unmarked numbers are prime.

~~~~exercism/note
The tests don't check that you've implemented the algorithm, only that you've come up with the correct list of primes.
To check you are implementing the Sieve correctly, a good first test is to check that you do not use division or remainder operations.
~~~~

## Example

Let's say you're finding the primes less than or equal to 10.

- List out 2, 3, 4, 5, 6, 7, 8, 9, 10, leaving them all unmarked.
- 2 is unmarked and is therefore a prime.
Mark 4, 6, 8 and 10 as "not prime".
- 3 is unmarked and is therefore a prime.
Mark 6 and 9 as not prime _(marking 6 is optional - as it's already been marked)_.
- 4 is marked as "not prime", so we skip over it.
- 5 is unmarked and is therefore a prime.
Mark 10 as not prime _(optional - as it's already been marked)_.
- 6 is marked as "not prime", so we skip over it.
- 7 is unmarked and is therefore a prime.
- 8 is marked as "not prime", so we skip over it.
- 9 is marked as "not prime", so we skip over it.
- 10 is marked as "not prime", so we stop as there are no more numbers to check.

You've examined all numbers and found 2, 3, 5, and 7 are still unmarked, which means they're the primes less than or equal to 10.
38 changes: 38 additions & 0 deletions exercises/practice/sieve/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@echo off
setlocal enabledelayedexpansion

set bitmap=
set n=Y
set "limit=%~1"
set "result="
set /a test=3,npri=3

if %limit% geq 2 set "result=%result% 2"
if %limit% geq 3 set "result=%result% 3"

:nextpri
set /a test+=2,index=0,div=3
if %test% LSS 8000 set bitmap=%bitmap%%n%
if %test% gtr 64000000 exit /b

:nextest
if "!bitmap:~%index%,1!"=="N" goto nextdiv
set /a resi=!test!%%!div!
if %resi% equ 0 set n=N& goto nextpri

:nextdiv
set /a index+=1, div+=2
set /a div2=div*div
if %test% gtr %limit% goto :end
if %div2% gtr %test% (
set n=Y
set "result=%result% %test%"
set /a npri+=1
goto nextpri)
goto nextest

:end
if defined result (
set result=%result:~1%
echo !result!
)
16 changes: 16 additions & 0 deletions exercises/practice/sieve/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["GroophyLifefor", "Antoni Gual"],
"files": {
"solution": [
"Sieve.bat"
],
"test": [
"SieveTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Use the Sieve of Eratosthenes to find all the primes from 2 up to a given number.",
"source": "Sieve of Eratosthenes at Wikipedia"
}
13 changes: 13 additions & 0 deletions exercises/practice/sieve/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[af9ffe10-dc13-42d8-a742-e7bdafac449d]
description = "Say Hi!"
10 changes: 10 additions & 0 deletions exercises/practice/sieve/Sieve.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@echo off
setlocal enabledelayedexpansion

set "limit=%~1"
set "result="

REM Your code goes here


echo %result%
109 changes: 109 additions & 0 deletions exercises/practice/sieve/SieveTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
@echo off
REM ---------------------------------------------------
REM Sieve Unit Testing
REM ---------------------------------------------------

:Main
REM Initalize result variable
set "slug=Sieve"

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/grains/canonical-data.json
REM --------------------
set "expected="
set "if_success=Test passed"
set "if_failed=Test failed: no primes under two"
CALL :Assert "1"

set "expected=2"
set "if_success=Test passed"
set "if_failed=Test failed: find first prime"
CALL :Assert "2"

set "expected=2 3 5 7"
set "if_success=Test passed"
set "if_failed=Test failed: find primes up to 10"
CALL :Assert "10"

set "expected=2 3 5 7 11 13"
set "if_success=Test passed"
set "if_failed=Test failed: limit is prime"
CALL :Assert "13"

set "expected=2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997"
set "if_success=Test passed"
set "if_failed=Test failed: find primes up to 1000"
CALL :Assert "1000"

REM --------------------
REM Test Case End /\/\/\
REM --------------------

CALL :ResolveStatus
exit /b %errorlevel%
REM End of Main

REM ---------------------------------------------------
REM Assert [..Parameters(up to 9)]
REM ---------------------------------------------------
GOTO :End REM Prevents the code below from being executed
:Assert
set "stdout="

REM Run the program and capture the output then delete the file
CALL %slug%.bat %~1 %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9 > stdout.bin 2>&1
set /p stdout=<stdout.bin
del stdout.bin

REM Check if the result is correct
if "%stdout%" == "%expected%" (
if defined if_success (
echo %if_success%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is correct, exit with code 0
set /a successCount+=1
exit /b 0
) else (
if defined if_failed (
echo %if_failed%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is incorrect, exit with code 1
set /a failCount+=1
exit /b 1
)
GOTO :EOF REM Go back to the line after the call to :Assert

:Initialize
REM It's for initialize, not about checking empty file
set "successCount=0"
set "failCount=0"
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile

:ResolveStatus
set "status="
if %failCount% gtr 0 (
REM status: Fail
REM message: The test failed.
exit /b 1

) else (
REM status: Pass
exit /b 0

)
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson

:End

0 comments on commit 51b1221

Please sign in to comment.