Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

algorithm: eliminate some redundant comparisons

This is only a minor optimization, but it's cleaner looking,
anyway. I figure it's worthwhile.

Signed-off-by: Steven Noonan <steven@uplinklabs.net>
  • Loading branch information...
commit 722da20b58f1d2c7243fea4792b2012c0522e896 1 parent 01d9809
@tycho authored
View
8 genprime.c
@@ -13,14 +13,12 @@ BOOL isprime(prime_t x)
prime_t lim, y;
if (x < 2)
return FALSE;
- if (x < 4)
- return TRUE;
- if (x == 5)
+ if (x == 2)
return TRUE;
if (x % 2 == 0)
return FALSE;
- if (x % 5 == 0)
- return FALSE;
+ if (x < 9)
+ return TRUE;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return FALSE;
View
8 genprime.cpp
@@ -13,14 +13,12 @@ class GenPrime
prime_t lim, y;
if (x < 2)
return false;
- if (x < 4)
- return true;
- if (x == 5)
+ if (x == 2)
return true;
if (x % 2 == 0)
return false;
- if (x % 5 == 0)
- return false;
+ if (x < 9)
+ return true;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return false;
View
8 genprime.cs
@@ -15,14 +15,12 @@ private static bool isprime(ulong x)
ulong lim, y;
if (x < 2)
return false;
- if (x < 4)
- return true;
- if (x == 5)
+ if (x == 2)
return true;
if (x % 2 == 0)
return false;
- if (x % 5 == 0)
- return false;
+ if (x < 9)
+ return true;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return false;
View
10 genprime.f90
@@ -45,11 +45,7 @@ function isprime(x) result (res)
res = .false.
return
end if
- if(x.lt.4) then
- res = .true.
- return
- end if
- if(x.eq.5) then
+ if(x.eq.2) then
res = .true.
return
end if
@@ -57,8 +53,8 @@ function isprime(x) result (res)
res = .false.
return
end if
- if(modulo(x,5).eq.0) then
- res = .false.
+ if(x.lt.9) then
+ res = .true.
return
end if
if(modulo(x + 1, 6) .ne. 0) then
View
8 genprime.html
@@ -12,14 +12,12 @@
var lim;
if (x < 2)
return false;
- if (x < 4)
- return true;
- if (x == 5)
+ if (x == 2)
return true;
if (x % 2 == 0)
return false;
- if (x % 5 == 0)
- return false;
+ if (x < 9)
+ return true;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return false;
View
10 genprime.java
@@ -4,14 +4,12 @@ private static boolean isprime(long x)
long lim, y;
if (x < 2)
return false;
- if (x < 4)
- return true;
- if (x == 5)
+ if (x == 2)
return true;
if (x % 2 == 0)
return false;
- if (x % 5 == 0)
- return false;
+ if (x < 9)
+ return true;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return false;
@@ -23,7 +21,7 @@ private static boolean isprime(long x)
}
return true;
}
-
+
private static long genprime(long max)
{
long count = 0,
View
11 genprime.lua
@@ -2,17 +2,14 @@ function isprime(x)
if x < 2 then
return false
end
- if x < 4 then
- return true
- end
- if x == 5 then
- return true
+ if x == 2 then
+ return true
end
if x % 2 == 0 then
return false
end
- if x % 5 == 0 then
- return false
+ if x < 9 then
+ return true
end
if (x + 1) % 6 ~= 0 then
if (x - 1) % 6 ~= 0 then
View
8 genprime.m
@@ -17,14 +17,12 @@ - (BOOL)isprime:(prime_t)x
prime_t lim, y;
if (x < 2)
return NO;
- if (x < 4)
- return YES;
- if (x == 5)
+ if (x == 2)
return YES;
if (x % 2 == 0)
return NO;
- if (x % 5 == 0)
- return NO;
+ if (x < 9)
+ return YES;
if ((x + 1) % 6 != 0)
if ((x - 1) % 6 != 0)
return NO;
View
8 genprime.php
@@ -5,14 +5,12 @@ function isprime($x)
{
if ($x < 2)
return false;
- if ($x < 4)
- return true;
- if ($x == 5)
+ if ($x == 2)
return true;
if ($x % 2 == 0)
return false;
- if ($x % 5 == 0)
- return false;
+ if ($x < 9)
+ return true;
if (($x + 1) % 6 != 0)
if (($x - 1) % 6 != 0)
return false;
View
9 genprime.pl
@@ -11,17 +11,14 @@ sub isprime {
if ($x < 2) {
return 0;
}
- if ($x < 4) {
- return 1;
- }
- if ($x == 5) {
+ if ($x == 2) {
return 1;
}
if ($x % 2 == 0) {
return 0;
}
- if ($x % 5 == 0) {
- return 0;
+ if ($x < 9) {
+ return 1;
}
if (($x + 1) % 6 != 0) {
if (($x - 1) % 6 != 0) {
View
8 genprime.py
@@ -7,14 +7,12 @@
def isprime(x):
if x < 2:
return False
- if x < 4:
- return True
- if x == 5:
+ if x == 2:
return True
if x % 2 == 0:
return False
- if x % 5 == 0:
- return False
+ if x < 9:
+ return True
if (x + 1) % 6 != 0:
if (x - 1) % 6 != 0:
return False
View
9 genprime.rb
@@ -4,17 +4,14 @@ def isprime(x)
if x < 2
return 0
end
- if x < 4
- return 1
- end
- if x == 5
+ if x == 2
return 1
end
if x % 2 == 0
return 0
end
- if x % 5 == 0
- return 0
+ if x < 9
+ return 1
end
if (x + 1) % 6 != 0
if (x - 1) % 6 != 0
Please sign in to comment.
Something went wrong with that request. Please try again.