Add ranges.

This commit is contained in:
Stephen Chung
2021-12-15 12:06:17 +08:00
parent 7251f34bce
commit ef14079c61
35 changed files with 1206 additions and 269 deletions

View File

@@ -19,7 +19,7 @@ print("Ready... Go!");
let result;
let now = timestamp();
for n in range(0, REPEAT) {
for n in 0..REPEAT {
result = fib(TARGET);
}

View File

@@ -8,7 +8,7 @@ let now = timestamp();
let list = [];
for i in range(0, MAX) {
for i in 0..MAX {
list.push(i);
}

View File

@@ -15,8 +15,8 @@ fn mat_gen() {
const tmp = 1.0 / n / n;
let m = new_mat(n, n);
for i in range(0, n) {
for j in range(0, n) {
for i in 0..n {
for j in 0..n {
m[i][j] = tmp * (i - j) * (i + j);
}
}
@@ -27,19 +27,19 @@ fn mat_gen() {
fn mat_mul(a, b) {
let b2 = new_mat(a[0].len, b[0].len);
for i in range(0, a[0].len) {
for j in range(0, b[0].len) {
for i in 0..a[0].len {
for j in 0..b[0].len {
b2[j][i] = b[i][j];
}
}
let c = new_mat(a.len, b[0].len);
for i in range(0, c.len) {
for j in range(0, c[i].len) {
for i in 0..c.len {
for j in 0..c[i].len {
c[i][j] = 0.0;
for z in range(0, a[i].len) {
for z in 0..a[i].len {
c[i][j] += a[i][z] * b2[j][z];
}
}
@@ -55,7 +55,7 @@ const b = mat_gen();
const c = mat_mul(a, b);
/*
for i in range(0, SIZE) {
for i in 0..SIZE) {
print(c[i]);
}
*/

View File

@@ -5,21 +5,21 @@ let now = timestamp();
const MAX_NUMBER_TO_CHECK = 1_000_000; // 9592 primes <= 100000
let prime_mask = [];
prime_mask.pad(MAX_NUMBER_TO_CHECK, true);
prime_mask.pad(MAX_NUMBER_TO_CHECK + 1, true);
prime_mask[0] = false;
prime_mask[1] = false;
let total_primes_found = 0;
for p in range(2, MAX_NUMBER_TO_CHECK) {
for p in 2..=MAX_NUMBER_TO_CHECK {
if !prime_mask[p] { continue; }
//print(p);
total_primes_found += 1;
for i in range(2 * p, MAX_NUMBER_TO_CHECK, p) {
for i in range(2 * p, MAX_NUMBER_TO_CHECK + 1, p) {
prime_mask[i] = false;
}
}