Ejemplo n.º 1
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#	
#   You can contact me at [email protected] 
#   Visit my wiki at http://san-ss.is-a-geek.com.ar
########################################################################

from itertools import permutations, combinations, chain
from CommonFunctions import find_primes_less_than

primes = find_primes_less_than(32000)

def is_prime(n):
    if n == 1:
        return False
    limit = int(n ** 0.5)
    for p in primes:
        if p > limit:
            return True
        if n % p == 0:
            return False
    return True

len_key = {}
len_dig_key = {}
Ejemplo n.º 2
0
def determine_prime(n):
    if n in primes:
        return True
    if n in non_primes:
        return False
    if is_prime(n):
        primes.add(n)
        return True
    else:
        non_primes.add(n)
        return False

if __name__ == '__main__':
    list_of_sets = []
    maxim = 0
    for prime in find_primes_less_than(10000)[1:]:
        for i in range(len(list_of_sets)):
            s = list_of_sets[i]
            enter = True
            str_prime = str(prime)
            for n in s:
                str_n = str(n)
                if not determine_prime(int(str_n + str_prime)) or not determine_prime(int(str_prime + str_n)):
                    enter = False
                    break
            if enter:
                newset = s.copy()
                newset.add(prime)
                if len(newset) == 5:
                    print("The result is:", sum(newset))
                    exit(0)
Ejemplo n.º 3
0
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#    
#   You can contact me at [email protected]
#   Visit my wiki at http://san-ss.wikidot.com
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import islice, count

primes = set(find_primes_less_than(1000000))

def is_truncable(i):
    if i < 10:
        return False
    tmp = str(i)
    for x in range(len(tmp)):
        if (int(tmp[x:]) not in primes) or (int(tmp[:x + 1]) not in primes):
            return False
    return True

if __name__ == '__main__':
    result = sum(islice(filter(is_truncable, primes), 11))
    print("The result is:", result)
    
Ejemplo n.º 4
0
    if n in primes:
        return True
    if n in non_primes:
        return False
    if is_prime(n):
        primes.add(n)
        return True
    else:
        non_primes.add(n)
        return False


if __name__ == '__main__':
    list_of_sets = []
    maxim = 0
    for prime in find_primes_less_than(10000)[1:]:
        for i in range(len(list_of_sets)):
            s = list_of_sets[i]
            enter = True
            str_prime = str(prime)
            for n in s:
                str_n = str(n)
                if not determine_prime(
                        int(str_n + str_prime)) or not determine_prime(
                            int(str_prime + str_n)):
                    enter = False
                    break
            if enter:
                newset = s.copy()
                newset.add(prime)
                if len(newset) == 5:
Ejemplo n.º 5
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://wiki.san-ss.com.ar
########################################################################

from functools import reduce
from CommonFunctions import find_primes_less_than

primes = find_primes_less_than(50)

mult = lambda x: reduce(lambda y, z: y * z, x, 1)

translate = lambda x: mult(primes[i]**((x[i] - 1) // 2) for i in range(len(x)))


def generator(limit):
    l = [1] * 14
    while l[13] < limit:
        i = 13
        while i > 0 and (l[i] == l[i - 1]):
            l[i] = 1
            i -= 1
        l[i] += 2
        yield l
Ejemplo n.º 6
0
        for mask in permutations(zeros + ones):
            masks.add(mask)
    return masks

def apply_mask(n, mask):
    str_n = str(n)
    result = ""
    remain = ""
    for i in range(len(mask)):
        result += mask[i] and str_n[i] or ""
        remain += (not mask[i]) and str_n[i] or ""
    return result, int(remain)
    
if __name__ == '__main__':

    primes = find_primes_less_than(1000000)
    length = 0
    result = None
    for prime in takewhile(lambda x: result is None, primes):
        if len(str(prime)) > length:
            length = len(str(prime))
            masks = build_masks(length)
            groups = {}
        for mask in masks:
            res, remain = apply_mask(prime, mask)
            if min(res) == max(res):
                if mask not in groups:
                    groups[mask] = {}
                if remain not in groups[mask]:
                    groups[mask][remain] = []
                groups[mask][remain].append(prime)
Ejemplo n.º 7
0
    'x' : set()
    }

def sam_clock(n):
    global numbers
    result = 0
    while n >= 10:
        result += sum(len(numbers[c]) for c in str(n)) * 2
        n = sum(int(c) for c in str(n))
    result += sum(len(numbers[c]) for c in str(n)) * 2
    return result

def max_clock(n):
    global numbers
    result = 0
    prev_n = 'x' * len(str(n))
    while n >= 10:
        str_n = 'x' * (len(prev_n) - len(str(n))) + str(n)
        result += sum(len((numbers[a] | numbers[b]) - (numbers[a] & numbers[b])) for a,b in zip(prev_n, str_n))
        prev_n = str(n)
        n = sum(int(c) for c in str(n))
    str_n = 'x' * (len(prev_n) - len(str(n))) + str(n)
    result += sum(len((numbers[a] | numbers[b]) - (numbers[a] & numbers[b])) for a,b in zip(prev_n, str_n))
    result += sum(len(numbers[a]) for a in str(n))
    return result

if __name__ == '__main__':
    primes = (x for x in find_primes_less_than(2 * (10 ** 7)) if x > 10 ** 7)
    result = sum(sam_clock(x) - max_clock(x) for x in primes)
    print("The result is: {0}", result)
Ejemplo n.º 8
0
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#	
#   You can contact me at [email protected]
#   Visit my wiki at http://wiki.san-ss.com.ar
#   Visit my blog at http://blog.san-ss.com.ar
########################################################################

from CommonFunctions import find_primes_less_than

LIMIT = 100000

if __name__ == '__main__':
    primes = find_primes_less_than(LIMIT)

    rads = [[1, i] for i in range(LIMIT+1)]
    for p in primes:
        for i in range(p, LIMIT+1, p):
            rads[i][0] *= p
    print("The result is:", sorted(rads)[10000][1])
Ejemplo n.º 9
0
from CommonFunctions import find_primes_less_than
from itertools import combinations

LIMIT = 120000

primes = find_primes_less_than(LIMIT)

p_to_n = {}
factors = [[set(), 1] for i in range(LIMIT)]
for p in primes:
    p_to_n[p] = set()
    for i in range(p, LIMIT, p):
        p_to_n[p].add(i)
        factors[i][0].add(p)
        factors[i][1] *= p

result = 0
for b in range(2, LIMIT - 1):
    possibles_a = set(range(1, min(LIMIT - b, b)))
    possibles_a.difference_update(*(p_to_n[p] for p in factors[b][0]))
    for a in possibles_a:
        c = a + b
        if factors[a][1] * factors[b][1] * factors[c][1] < c:
            result += c
print("Result:", result)
#~
#~ result = 0
#~ for c in range(3, LIMIT):
#~ for a, b in ((a, c - a) for a in range(1, c // 2 + 1)):
#~ if (factors[a][0] & factors[b][0]):
#~ continue
Ejemplo n.º 10
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://san-ss.is-a-geek.com.ar
########################################################################

from itertools import product, combinations, zip_longest
from CommonFunctions import find_primes_less_than

primes = find_primes_less_than(350000)


def is_prime(n):
    for p in primes:
        if p >= n:
            return True
        if n % p == 0:
            return False
    return True


def generator(n, d, rep):
    def filt_func(t):
        if t[0][0] == 0 and t[1][0] == 0:
            return False
Ejemplo n.º 11
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#	
#   You can contact me at [email protected] 
#   Visit my wiki at http://wiki.san-ss.com.ar
########################################################################

from functools import reduce
from CommonFunctions import find_primes_less_than

primes = find_primes_less_than(50)

mult = lambda x: reduce(lambda y, z: y * z, x, 1)

translate = lambda x: mult(primes[i] ** ((x[i] - 1) // 2) for i in range(len(x)))

def generator(limit):
    l = [1] * 14
    while l[13] < limit:
        i = 13
        while i > 0 and (l[i] == l[i-1]):
            l[i] = 1
            i -= 1
        l[i] += 2
        yield l
Ejemplo n.º 12
0
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://wiki.san-ss.com.ar
#   Visit my blog at http://blog.san-ss.com.ar
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import takewhile

primes = find_primes_less_than(6 * (10**6))
primes_set = set(primes)


def primes_in_range(from_, to_):

    p_limit = (to_ + 1)**(0.5)
    prime_set = set(range(from_, to_ + 1))
    for p in takewhile(lambda x: x <= p_limit, primes):
        mod = from_ % p
        start_n = from_ + (0 if mod == 0 else p - mod)
        for i in range(start_n, to_ + 1, p):
            prime_set.discard(i)

    return prime_set
Ejemplo n.º 13
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#	
#   You can contact me at [email protected] 
#   Visit my wiki at http://san-ss.is-a-geek.com.ar
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import count, takewhile

primes = find_primes_less_than(int(1000000 ** 0.5))

def is_prime(n):
    limit = n ** 0.5
    for p in primes:
        if p > limit:
            return True
        if n % p == 0:
            return False
    return True

if __name__ == '__main__':
    result = sum(1 for i in 
                    takewhile(
                        lambda x: x < 1000000, 
                        ((i + 1) ** 3 - i ** 3 for i in count(1))
Ejemplo n.º 14
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://san-ss.is-a-geek.com.ar
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import count, takewhile

primes = find_primes_less_than(int(1000000**0.5))


def is_prime(n):
    limit = n**0.5
    for p in primes:
        if p > limit:
            return True
        if n % p == 0:
            return False
    return True


if __name__ == '__main__':
    result = sum(1
                 for i in takewhile(lambda x: x < 1000000, ((i + 1)**3 - i**3
Ejemplo n.º 15
0
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#	
#   You can contact me at [email protected]
#   Visit my wiki at http://wiki.san-ss.com.ar
#   Visit my blog at http://blog.san-ss.com.ar
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import takewhile

primes = find_primes_less_than(6 * (10 ** 6))
primes_set = set(primes)

def primes_in_range(from_, to_):

    p_limit = (to_ + 1) ** (0.5)
    prime_set = set(range(from_, to_ + 1))
    for p in takewhile(lambda x: x <= p_limit, primes):
        mod = from_ % p
        start_n = from_ + (0 if mod == 0 else p - mod)
        for i in range(start_n, to_ + 1, p):
            prime_set.discard(i)
    
    return prime_set

sum_n = lambda n: n * (n + 1) // 2
Ejemplo n.º 16
0
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://san-ss.wikidot.com
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import dropwhile

is_anagram = lambda x, y: sorted(str(x)) == sorted(str(y))

if __name__ == '__main__':
    primes = find_primes_less_than(10000)
    primes_greater_1000 = dropwhile(lambda x: x <= 1000, primes)
    primes = set(primes)
    found_one = False
    for base in primes_greater_1000:
        for increment in range(1, ((10000 - base) // 2)):
            n1 = base + increment
            n2 = base + increment * 2
            if n1 in primes and n2 in primes and is_anagram(
                    base, n1) and is_anagram(base, n2):
                if found_one:
                    print("The result is:", str(base) + str(n1) + str(n2))
                    exit(0)
                found_one = True
Ejemplo n.º 17
0
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#	
#   You can contact me at [email protected]
#   Visit my wiki at http://wiki.san-ss.com.ar
#   Visit my blog at http://blog.san-ss.com.ar
########################################################################

from CommonFunctions import find_primes_less_than
from fractions import Fraction

d = {}

primes = set(find_primes_less_than(501))
for i in range(1, 501):
    if i in primes:
        d[(i, 'P')] = Fraction(2, 3)
        d[(i, 'N')] = Fraction(1, 3)
    else:
        d[(i, 'P')] = Fraction(1, 3)
        d[(i, 'N')] = Fraction(2, 3)

def calc_prob(i, string):
    if (i, string) in d:
        return d[(i, string)]
    
    if (i == 1):
        prob = d[(i, string[0])] * calc_prob(i + 1, string[1:])
    elif (i == 500):
Ejemplo n.º 18
0
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://wiki.san-ss.com.ar
#   Visit my blog at http://blog.san-ss.com.ar
########################################################################

from CommonFunctions import find_primes_less_than
from fractions import Fraction

d = {}

primes = set(find_primes_less_than(501))
for i in range(1, 501):
    if i in primes:
        d[(i, 'P')] = Fraction(2, 3)
        d[(i, 'N')] = Fraction(1, 3)
    else:
        d[(i, 'P')] = Fraction(1, 3)
        d[(i, 'N')] = Fraction(2, 3)


def calc_prob(i, string):
    if (i, string) in d:
        return d[(i, string)]

    if (i == 1):
        prob = d[(i, string[0])] * calc_prob(i + 1, string[1:])
Ejemplo n.º 19
0
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#   You can contact me at [email protected]
#   Visit my wiki at http://san-ss.wikidot.com
########################################################################

from CommonFunctions import find_primes_less_than
from itertools import islice, count

primes = set(find_primes_less_than(1000000))


def is_truncable(i):
    if i < 10:
        return False
    tmp = str(i)
    for x in range(len(tmp)):
        if (int(tmp[x:]) not in primes) or (int(tmp[:x + 1]) not in primes):
            return False
    return True


if __name__ == '__main__':
    result = sum(islice(filter(is_truncable, primes), 11))
    print("The result is:", result)
Ejemplo n.º 20
0
    while n >= 10:
        result += sum(len(numbers[c]) for c in str(n)) * 2
        n = sum(int(c) for c in str(n))
    result += sum(len(numbers[c]) for c in str(n)) * 2
    return result


def max_clock(n):
    global numbers
    result = 0
    prev_n = 'x' * len(str(n))
    while n >= 10:
        str_n = 'x' * (len(prev_n) - len(str(n))) + str(n)
        result += sum(
            len((numbers[a] | numbers[b]) - (numbers[a] & numbers[b]))
            for a, b in zip(prev_n, str_n))
        prev_n = str(n)
        n = sum(int(c) for c in str(n))
    str_n = 'x' * (len(prev_n) - len(str(n))) + str(n)
    result += sum(
        len((numbers[a] | numbers[b]) - (numbers[a] & numbers[b]))
        for a, b in zip(prev_n, str_n))
    result += sum(len(numbers[a]) for a in str(n))
    return result


if __name__ == '__main__':
    primes = (x for x in find_primes_less_than(2 * (10**7)) if x > 10**7)
    result = sum(sam_clock(x) - max_clock(x) for x in primes)
    print("The result is: {0}", result)