-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe75b18
commit dcbc750
Showing
6 changed files
with
172 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,7 @@ | |
(file poly.c) | ||
(file poly2.c) | ||
(file dobble.c) | ||
(file primes.c) | ||
(file primes2.c) | ||
(package owi)) | ||
(files README.md)) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#define MAX_SIZE 100 | ||
|
||
#include <owi.h> | ||
#include <stdlib.h> | ||
|
||
/*@ requires 2 <= n <= MAX_SIZE; | ||
requires \valid(is_prime + (0 .. (n - 1))); | ||
ensures \forall integer i; 0 <= i < n ==> | ||
(is_prime[i] == 1 <==> | ||
(i >= 2 && \forall integer j; 2 <= j < i ==> i % j != 0)); | ||
*/ | ||
void primes(int *is_prime, int n) { | ||
for (int i = 0; i < n; ++i) is_prime[i] = 1; | ||
for (int i = 2; i * i < n; ++i) { | ||
if (is_prime[i] == 0) continue; | ||
for (int j = i; i * j < n; ++j) { | ||
is_prime[i * j] = 0; | ||
} | ||
} | ||
} | ||
|
||
int main(void) { | ||
int *is_prime; | ||
is_prime = malloc(MAX_SIZE * sizeof(int)); | ||
|
||
int n = owi_i32(); | ||
owi_assume(n >= 2); | ||
owi_assume(n <= MAX_SIZE); | ||
|
||
primes(is_prime, n); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#define MAX_SIZE 100 | ||
|
||
#include <owi.h> | ||
#include <stdlib.h> | ||
|
||
/*@ requires 2 <= n <= MAX_SIZE; | ||
requires \valid(is_prime + (0 .. (n - 1))); | ||
ensures \forall integer i; 0 <= i < n ==> | ||
(is_prime[i] == 1 <==> | ||
(i >= 2 && \forall integer j; 2 <= j < i ==> i % j != 0)); | ||
*/ | ||
void primes(int *is_prime, int n) { | ||
for (int i = 0; i < n; ++i) is_prime[i] = 1; | ||
is_prime[0] = is_prime[1] = 0; | ||
for (int i = 2; i * i < n; ++i) { | ||
if (is_prime[i] == 0) continue; | ||
for (int j = i; i * j < n; ++j) { | ||
is_prime[i * j] = 0; | ||
} | ||
} | ||
} | ||
|
||
int main(void) { | ||
int *is_prime; | ||
is_prime = malloc(MAX_SIZE * sizeof(int)); | ||
|
||
int n = owi_i32(); | ||
owi_assume(n >= 2); | ||
owi_assume(n <= MAX_SIZE); | ||
|
||
primes(is_prime, n); | ||
return 0; | ||
} |
is_prime[i] == 1
could be replaced byis_prime[i]
?