Created
April 11, 2013 21:34
-
-
Save labeneator/5367379 to your computer and use it in GitHub Desktop.
Better prime finding methods? i.e with a better big-O performance?
This file contains hidden or 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
require("ggplot2") | |
# data load | |
n_squared = read.csv("nth.csv", header=T) | |
n_modulo = read.csv("nth_modulo.csv", header=T) | |
n_modulo$nth_log <- log(n_modulo$nth) | |
n_modulo$time_log <- log(n_modulo$time) | |
n_squared$time_log <- log(n_squared$time) | |
n_squared$nth_log <- log(n_squared$nth_prime) | |
png("log_performance.png",800,600) | |
ggplot(n_squared, aes(nth_log, time_log, colour="methods")) + geom_line(aes(colour="scan")) + geom_line(data=n_modulo, aes(nth_log, time_log,colour="modulo")) + ggtitle("Performance of prime finding algorithms (log)") | |
dev.off() | |
png("raw_performance.png",800,600) | |
ggplot(n_squared, aes( colour="methods")) + geom_line(aes(nth_prime, time, colour="scan")) + geom_line(data=n_modulo, aes(nth_prime, time,colour="modulo")) + ggtitle("Performance of prime finding algorithms") | |
dev.off() | |
png("logn_performance.png",800,600) | |
ggplot(n_squared, aes( colour="methods")) + geom_line(aes(nth_log, time, colour="scan")) + geom_line(data=n_modulo, aes(nth_log, time,colour="modulo")) + ggtitle("Performance of prime finding algorithms (logn)") | |
dev.off() |
This file contains hidden or 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
import time | |
import math | |
from sets import Set | |
def is_it_prime(n): | |
largest_candidate = int(math.ceil(n/2)) | |
candidates = range(2, largest_candidate + 1) | |
#print "%d => %s" % (n, candidates) | |
for i in candidates: | |
if not n % i: | |
#print "Not prime: %d => %d" % (n, i) | |
return False | |
return True | |
def nth_prime(n): | |
primes = [2] | |
current_number = 3 | |
while len(primes) < n: | |
if is_it_prime(current_number): | |
primes.append(current_number) | |
current_number += 2 | |
return primes[n-1] | |
def nth_prime_modulo(n): | |
primes = [2] | |
current_number = 3 | |
while len(primes) < n: | |
is_prime = 1 | |
for prime in primes: | |
if not current_number % prime: | |
is_prime = 0 | |
break | |
if is_prime: | |
primes.append(current_number) | |
current_number += 2 | |
return primes[n-1] | |
def generate_nth_prime_modulo(n): | |
start = time.time() | |
nth_prime_modulo(n) | |
total_time = time.time() - start | |
#print "%f for %d" % (total_time, n) | |
return total_time | |
def generate_nth_prime(n): | |
start = time.time() | |
nth_prime(n) | |
total_time = time.time() - start | |
return total_time | |
n = 3000 | |
fp = open("data/nth.csv", "w") | |
fp.write("nth_prime,time\n") | |
for i in range(0, n, 100): | |
fp.write("%d,%f\n" % (i, generate_nth_prime(i))) | |
fp.close() | |
fp = open("data/nth_modulo.csv", "w") | |
fp.write("nth_prime,time\n") | |
for i in range(0, n, 100): | |
fp.write("%d,%f\n" % (i, generate_nth_prime_modulo(i))) | |
fp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment