2014.5.8
第六题(python):
sum_of_squares = 0 sum_of_number = 0 for i in range(1, 101): sum_of_squares = sum_of_squares + i*i for i in range(1, 101): sum_of_number = sum_of_number + i print sum_of_number**2 - sum_of_squares
2014.5.8
第七题(python):
import math import random import sys sys.setrecursionlimit(1000000) # def is_prime(num): # i = 2 # isprime = True # while i <= int(math.sqrt(num)): # if num % i == 0: # isprime = False # break # i = i + 1 # return isprime # def is_prime(num): # if num % 2 == 0: # return False; # i = 3 # isprime = True # while i <= int(math.sqrt(num)): # if num % i == 0: # isprime = False # break; # i = i + 2 # return isprime # prime_list = [2, 3] # def is_prime(num): # isprime = True # i = 0 # while i < len(prime_list): # if num % prime_list[i] == 0: # isprime = False # break # i = i + 1 # return isprime prime_list = [2, 3] def is_prime(num): isprime = True i = 0 while i < len(prime_list) and prime_list[i] <= int(math.sqrt(num)): if num % prime_list[i] == 0: isprime = False break i = i + 1 return isprime # def is_prime(num): # f = 5 # isprime = True # r = int(math.sqrt(num)) + 1 # if num % 2 == 0 or num % 3 == 0: # return False # while f <= r: # if num % f == 0: # isprime = False # elif num % (f + 2) == 0: # isprime = False # f = f + 6 # return isprime # Wilson' theroem # def factorial(n): # if (n == 1): # return n # else: # return n * factorial(n - 1) # def is_prime(num): # if (factorial(num - 1) + 1) % num == 0: # return True # else: # return False # Fermat's little theroem # def FermatPrimalityTest(number): # for time in range(10): # randomNumber = random.randint(2, number - 1) # if ( pow(randomNumber, number-1, number) != 1 ): # return False # return True number_of_prime = 2 num = 5 while True: if is_prime(num): prime_list.append(num) number_of_prime = number_of_prime + 1 if number_of_prime == 40001: print num break # if is_prime(num) == False and FermatPrimalityTest(num) == True: # print num num = num + 2
2014.5.14
第八题(python):
num = '''73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450''' num = num.replace('\n','').replace(' ','') greatest = 0 for i in range(0, 996): temp = int(num[i:i + 1]) * int(num[i + 1:i + 2]) * \ int(num[i + 2:i + 3]) * int(num[i + 3:i + 4]) * \ int(num[i + 4:i + 5]) if temp > greatest: greatest = temp print greatest
2014.5.14
第九题(python):
import math a = 1 while a < 333: b = a + 1 while b < 1000: c = math.sqrt(a*a + b*b) if a + b + c == 1000: print a, b, c print a*b*c b = b + 1 a = a + 1
2014.5.14
第十题(python):
注释掉的是其他一些方法。
import math # def is_prime(num): # i = 2 # isprime = True # while i <= int(math.sqrt(num)): # if num % i == 0: # isprime = False # break # i = i + 1 # return isprime # prime_list = [2, 3] # def is_prime(num): # isprime = True # i = 0 # while i < len(prime_list) and prime_list[i] <= int(math.sqrt(num)): # if num % prime_list[i] == 0: # isprime = False # break # i = i + 1 # return isprime # sum = 5 # num = 5 # while num < 2000000: # if is_prime(num): # prime_list.append(num) # sum = sum + num # num = num + 2 # print sum # Sieve of Eratosthenes sieve = [False, False] for i in range(0, 2000001): sieve.append(True) for i in range(2, int(math.sqrt(2000000) + 1)): if sieve[i] == True: j = i*i while j <= 2000000: sieve[j] = False j = j + i sum = 0 for i in range(2, 2000001): if sieve[i] == True: sum = sum + i print sum第十题(java):
class ProjectEuler10{ public static void main(String [] args){ long sum = 2; long num = 3; while(num < 2000000){ if(isPrime(num)){ sum = sum + num; } num = num + 2; } System.out.println(sum); } public static boolean isPrime(long num){ long i = 2; boolean isprime = true; while(i <= Math.sqrt(num)){ if(num % i == 0){ isprime = false; break; } i++; } return isprime; } }
没有评论:
发表评论