Skip to content

Instantly share code, notes, and snippets.

@sankage
Created June 23, 2012 21:39
Show Gist options
  • Save sankage/2980111 to your computer and use it in GitHub Desktop.
Save sankage/2980111 to your computer and use it in GitHub Desktop.
Prime Numbers
class Prime
constructor: (@prime = 2) ->
@history = [@prime]
isPrime: (num) ->
result = true
if num isnt 2
if num % 2 is 0
result = false
else
for x in [3..Math.sqrt(num)] by 2
result = false if num % x is 0
result
nextPrime: ->
@prime += 1
@prime += 1 while not @isPrime(@prime)
@history.push @prime
@prime
primesUpTo: (num) ->
@history = [2]
@prime = 2
@nextPrime() while @prime < num
@history.pop()
@history
additiveFunctionCheck = (fn, test_nums) ->
result = true
for x in test_nums
for y in test_nums
result = false if fn(x+y) isnt fn(x) + fn(y)
result
# Test Cases
func_add = (int) -> int/2
func_not_add = (int) -> Math.floor Math.sqrt(int)
prime_numbers = (new Prime).primesUpTo(100)
additiveFunctionCheck(func_add, prime_numbers)
additiveFunctionCheck(func_not_add, prime_numbers)
var Prime, additiveFunctionCheck, func_add, func_not_add, prime_numbers;
Prime = (function() {
function Prime(prime) {
this.prime = prime != null ? prime : 2;
this.history = [this.prime];
}
Prime.prototype.isPrime = function(num) {
var result, x, _ref;
result = true;
if (num !== 2) {
if (num % 2 === 0) {
result = false;
} else {
for (x = 3, _ref = Math.sqrt(num); x <= _ref; x += 2) {
if (num % x === 0) result = false;
}
}
}
return result;
};
Prime.prototype.nextPrime = function() {
this.prime += 1;
while (!this.isPrime(this.prime)) {
this.prime += 1;
}
this.history.push(this.prime);
return this.prime;
};
Prime.prototype.primesUpTo = function(num) {
this.history = [2];
this.prime = 2;
while (this.prime < num) {
this.nextPrime();
}
this.history.pop();
return this.history;
};
return Prime;
})();
additiveFunctionCheck = function(fn, test_nums) {
var result, x, y, _i, _j, _len, _len2;
result = true;
for (_i = 0, _len = test_nums.length; _i < _len; _i++) {
x = test_nums[_i];
for (_j = 0, _len2 = test_nums.length; _j < _len2; _j++) {
y = test_nums[_j];
if (fn(x + y) !== fn(x) + fn(y)) result = false;
}
}
return result;
};
func_add = function(int) {
return int / 2;
};
func_not_add = function(int) {
return Math.floor(Math.sqrt(int));
};
prime_numbers = (new Prime).primesUpTo(100);
additiveFunctionCheck(func_add, prime_numbers);
additiveFunctionCheck(func_not_add, prime_numbers);
<?php
class Prime {
public function __consruct($prime = 2) {
while (!is_prime($prime)) {
$prime += 1;
}
$this->prime = $prime;
$this->history = array();
array_push($this->history, $prime);
}
public function is_prime($num) {
if ($num != 2)
if ($num % 2 == 0) {
return false;
} else {
for($x = 3; $x <= ceil(sqrt($num); $x += 2) {
if ($num % $x == 0) {
return false;
}
}
}
}
return true
}
public function next() {
$this->prime += 1;
while (!is_prime($this->prime)) {
$this->prime += 1;
}
array_push($this->history, $this->prime);
return $this->prime;
}
private function endc( $array ) {
return end( $array );
}
public function up_to($num) {
while ($this->prime < $num) {
$this->next();
}
if (endc($this->history) > $num) {
array_pop($this->history);
}
return $this->history;
}
}
function additiveFunctionCheck($fn, $test_nums) {
foreach($test_nums as $x) {
foreach($test_nums as $y) {
if (call_user_func($fn, $x+$y) != call_user_func($fn, $x) + call_user_func($fn, $y)){
return false;
}
}
}
return true;
}
// Test Cases
function func_add($int) {
return $int/2;
}
function func_not_add($int) {
return floor(sqrt($int));
}
$prime_numbers = (new Prime()).up_to(100);
additiveFunctionCheck('func_add', $prime_numbers);
additiveFunctionCheck('func_not_add', $prime_numbers);
?>
class Prime
attr_reader :prime, :history
def initialize(prime = 2)
prime += 1 while !prime?(prime)
@prime = prime
@history = [prime]
end
def prime?(num)
unless num == 2
if num % 2 == 0
return false
else
3.step(Math.sqrt(num).ceil,2) { |x| return false if num % x == 0 }
end
end
true
end
def succ
@prime += 1
@prime += 1 while !prime?(@prime)
@history << @prime
@prime
end
alias_method :next, :succ
def up_to(num)
num = num.to_i
succ while @prime < num
@history.pop if @history[-1] > num
@history
end
end
def additive_function?(test_nums)
test_nums.each do |x|
test_nums.each do |y|
return false unless yield(x+y) == yield(x) + yield(y)
end
end
true
end
# Test Cases
def func_add(int)
int*1.0/2
end
def func_not_add(int)
Math.sqrt(int).floor
end
prime_numbers = Prime.new.up_to(100)
# => [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]
additive_function?(prime_numbers) { |int| func_add(int) }
# => true
additive_function?(prime_numbers) { |int| func_not_add(int) }
# => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment