コード例 #1
0
ファイル: problem_1.py プロジェクト: gavinsykes/project-euler
def main():
    from sys import path as syspath
    from os import path

    syspath.append(path.dirname(path.dirname(__file__)))
    from pyfuncs import fullprint

    challenge = f'Find the sum of all the multiples of 3 or 5 below {x}:'
    fullprint(challenge, euler_1, x)
コード例 #2
0
ファイル: problem_6.py プロジェクト: gavinsykes/project-euler
challenge = 'Find the difference between the sum of the squares of the first {} natural numbers and the square of the sum:'

parser = argparse.ArgumentParser(description = 'Find the difference between the sum of the squares of the first x natural numbers and the square of the sum.')
parser.add_argument('--num', default = 10, type = int, help = 'Insert the number here, it must be a positive integer. It defaults to 10 to correspond with the Project Euler Problem at https://projecteuler.net/problem=6')
x = parser.parse_args().num

if ( (x < 1) and (not isinstance(x, int)) ):
  raise Exception(f'You entered {x}, which is neither an integer nor larger than 1!')
if (x < 1):
  raise Exception(f'You entered {x}, which is less than 1, please try a positive integer.')
if (not isinstance(x,int)):
  raise Exception(f'You entered {x}, which is not an integer, please try a positive integer.')

def euler_6(n):
  if ( (n < 1) or (not isinstance(n, int)) ):
    return None

  result = 0
  sumsquares = 0
  _sum = 0
  squaresum = 0
  for i in range(n):
    sumsquares += i ** 2
  for i in range(n):
    _sum += i
  squaresum = _sum ** 2
  result = squaresum - sumsquares
  return result

pyfuncs.fullprint(challenge,euler_6,x,__file__)
コード例 #3
0
    )
if (not isinstance(x, int)):
    raise Exception(
        f'You entered {x}, which is not an integer, please try a positive integer.'
    )


def euler_12(n):
    i = 1
    while (len(get_divisors(i)) < n + 1):
        i += 1
    return i


def get_divisors(n):
    divisors = list()
    if (not isinstance(n, int) or n < 1):
        return undefined

    for i in range(1, int(n / 2)):
        if (n % i == 0):
            divisors.append(i)
            divisors.append(int(n / i))

    l = list(dict.fromkeys(divisors))
    l.sort()
    return l


pyfuncs.fullprint(challenge, euler_12, x)
コード例 #4
0
import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import pyfuncs

challenge = ''

parser = argparse.ArgumentParser(description='')
parser.add_argument('--num', default=1000, type=int, help='')
x = parser.parse_args().num

if ((x < 1) and (not isinstance(x, int))):
    raise Exception(
        f'\033[1;31mYou entered {x}, which is neither an integer nor larger than 1!\033[0m'
    )
if (x < 1):
    raise Exception(
        f'\033[1;31mYou entered {x}, which is less than 1, please try a positive integer.\033[0m'
    )
if (not isinstance(x, int)):
    raise Exception(
        f'\033[1;31mYou entered {x}, which is not an integer, please try a positive integer.\033[0m'
    )

###
### Code goes here
###

pyfuncs.fullprint(challenge, function_name, x, __file__)
コード例 #5
0
ファイル: problem_9.py プロジェクト: gavinsykes/project-euler
import sys

sys.path.append('/home/gavin/project-euler')
import pyfuncs

challenge = 'There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.'

def euler_9(n):
  a = b = c = 1;
  for a in range(1,n-2):
    for b in range(1,n-a-1):
      c = n - a - b;
      if (pyfuncs.is_pythagorean_triple(a,b,c)):
        return(f'{a}, {b} and {c} make {a*b*c}')

pyfuncs.fullprint(challenge,euler_9,1000)
コード例 #6
0
ファイル: problem_2.py プロジェクト: gavinsykes/project-euler
def main():
    challenge = f'Find the sum of the even-valued Fibonacci terms up to {x}:'
    fullprint(challenge, euler_2, x)