Skip to content

Instantly share code, notes, and snippets.

View GrAndSE's full-sized avatar

Andrii Hryhoriev GrAndSE

  • Svitla
  • Ukraine, Sumy
View GitHub Profile
import abc
import collections
class BasePrime(collections.Sequence):
'''Base class for any primes number sequence
'''
__metaclass__ = abc.ABCMeta
@GrAndSE
GrAndSE / prime-finder.py
Created April 10, 2013 11:21
Trying to find a Nth prime number (for a http://projecteuler.net/problem=7)
def get_primes(start, end, primes=[]):
'''Get the numbers starting from start with the count based on primes
'''
sieve = range(start, end)
for prime in primes:
# Get the start value for primes striking
start_value = prime ** 2
if start_value > end:
break
if start_value < start:
@GrAndSE
GrAndSE / .vimrc
Created November 29, 2012 09:15
My .vimrc
syntax on
set tabstop=4
set shiftwidth=4
set si
set nu
set showmatch
set hlsearch
set incsearch
set lz
set listchars=tab:..
@GrAndSE
GrAndSE / ColorCanvas.py
Created August 15, 2012 17:02
soy.widgets.Canvas example
#!/usr/bin/env python3
from random import randint, random
from time import sleep
import soy
client = soy.Client()
tex = soy.textures.Texture()
tex.size = soy.atoms.Size((16, 16))
tex.smooth = False
can = soy.widgets.Canvas(client.window, tex)
@GrAndSE
GrAndSE / prime.hs
Created September 21, 2011 14:12
Prime numbers searching using Haskell
getDivisors num
| num < 1 = []
| otherwise = [x | x <- [n | n <- primeNumbers (num - 1), n <= ((floor.sqrt.fromIntegral) (num + 1))], num `mod` x == 0 ]
primeNumbers :: Integer -> [Integer]
primeNumbers 2 = [2]
primeNumbers num =
if getDivisors num == []
then primeNumbers (num - 1) ++ [num]
else primeNumbers (num - 1)
@GrAndSE
GrAndSE / prime-opt.py
Created September 21, 2011 14:10
Prime numbers searching using Python (with optimizations)
def prime(num):
primes = []
def is_prime(n):
limit = int(n**0.5) + 1
for i in primes:
if i > limit:
return True
elif n % i == 0:
return False
return True
@GrAndSE
GrAndSE / prime.py
Created September 21, 2011 14:08
Prime numbers searching using Python
def primes(num):
def is_prime(n):
limit = int(n**0.5) + 1
for i in range(2, limit):
if n % i == 0:
return False
return True
return [n for n in range(2, num) if is_prime(n)]
print primes(1000000)
@GrAndSE
GrAndSE / prime.cpp
Created September 21, 2011 14:04
Example of prime numbers searching in C++ using list
#include <list>
#include <iostream>
#include "math.h"
using namespace std;
bool isPrime(int n) {
for (int i = 2; i < (int) pow(n*1., 0.5) + 1; i++)
if (n % i == 0)
return false;
@GrAndSE
GrAndSE / prime-opt.cpp
Created September 21, 2011 14:02
Example of prime numbers searching in C++ using list (with ptimizations)
#include <list>
#include <iostream>
#include "math.h"
using namespace std;
bool isPrime(int n, list<int> &primes) {
for (list<int>::iterator iter = primes.begin(); iter != primes.end(); ++iter)
if (*iter > (int) pow(n*1., 0.5) + 1)
return true;
@GrAndSE
GrAndSE / enum.py
Created April 6, 2011 16:48
Simple python enumeration class
from collections import namedtuple
class Enum(tuple):
def __new__(cls, *sequential, **named):
new_cls = namedtuple(cls.__name__, sequential + tuple(named.keys()))
return new_cls._make(range(len(sequential))+named.values())
class ChoicedEnum(tuple):