Modernize Python 2 code to get ready for Python 3

This commit is contained in:
cclauss
2017-11-25 10:23:50 +01:00
parent a03b2eafc0
commit 4e06949072
95 changed files with 580 additions and 521 deletions

View File

@ -4,9 +4,14 @@ If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3,5,6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
'''
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
sum=0;
sum=0
for a in range(3,n):
if(a%3==0 or a%5==0):
sum+=a
print sum;
print(sum)

View File

@ -4,6 +4,11 @@ If we list all the natural numbers below 10 that are multiples of 3 or 5,
we get 3,5,6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
'''
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
sum = 0
terms = (n-1)/3
@ -12,4 +17,4 @@ terms = (n-1)/5
sum+= ((terms)*(10+(terms-1)*5))/2
terms = (n-1)/15
sum-= ((terms)*(30+(terms-1)*15))/2
print sum
print(sum)

View File

@ -7,6 +7,11 @@ Find the sum of all the multiples of 3 or 5 below N.
'''
This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.
'''
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
sum=0;
num=0;
@ -39,4 +44,4 @@ while(1):
if(num>=n):
break
sum+=num
print sum;
print(sum);

View File

@ -6,6 +6,12 @@ the first 10 terms will be:
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
e.g. for n=10, we have {2,8}, sum is 10.
'''
from __future__ import print_function
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
i=1; j=2; sum=0
@ -15,4 +21,4 @@ while(j<=n):
temp=i
i=j
j=temp+i
print sum
print(sum)

View File

@ -3,6 +3,7 @@ Problem:
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
'''
from __future__ import print_function
import math
@ -20,12 +21,12 @@ def isprime(no):
max=0
n=int(input())
if(isprime(n)):
print n
print(n)
else:
while (n%2==0):
n=n/2
if(isprime(n)):
print n
print(n)
else:
n1 = int(math.sqrt(n))+1
for i in range(3,n1,2):
@ -35,4 +36,4 @@ else:
break
elif(isprime(i)):
max=i
print max
print(max)

View File

@ -3,6 +3,7 @@ Problem:
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
'''
from __future__ import print_function
n=int(input())
prime=1
i=2
@ -13,4 +14,4 @@ while(i*i<=n):
i+=1
if(n>1):
prime=n
print prime
print(prime)

View File

@ -3,6 +3,7 @@ Problem:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers which is less than N.
'''
from __future__ import print_function
n=int(input())
for i in range(n-1,10000,-1):
temp=str(i)
@ -10,6 +11,6 @@ for i in range(n-1,10000,-1):
j=999
while(j!=99):
if((i%j==0) and (len(str(i/j))==3)):
print i
print(i)
exit(0)
j-=1

View File

@ -3,6 +3,7 @@ Problem:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers which is less than N.
'''
from __future__ import print_function
arr = []
for i in range(999,100,-1):
for j in range(999,100,-1):
@ -14,5 +15,5 @@ arr.sort()
n=int(input())
for i in arr[::-1]:
if(i<n):
print i
print(i)
exit(0)

View File

@ -3,6 +3,7 @@ Problem:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N?
'''
from __future__ import print_function
n = int(input())
i = 0
@ -16,5 +17,5 @@ while 1:
if(nfound==0):
if(i==0):
i=1
print i
print(i)
break

View File

@ -8,6 +8,7 @@ The square of the sum of the first ten natural numbers is,
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first N natural numbers and the square of the sum.
'''
from __future__ import print_function
suma = 0
sumb = 0
@ -16,4 +17,4 @@ for i in range(1,n+1):
suma += i**2
sumb += i
sum = sumb**2 - suma
print sum
print(sum)

View File

@ -8,8 +8,9 @@ The square of the sum of the first ten natural numbers is,
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
Find the difference between the sum of the squares of the first N natural numbers and the square of the sum.
'''
from __future__ import print_function
n = int(input())
suma = n*(n+1)/2
suma **= 2
sumb = n*(n+1)*(2*n+1)/6
print suma-sumb
print(suma-sumb)

View File

@ -3,6 +3,7 @@ By listing the first six prime numbers:
2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the Nth prime number?
'''
from __future__ import print_function
from math import sqrt
def isprime(n):
if (n==2):
@ -26,4 +27,4 @@ while(i!=n):
j+=2
if(isprime(j)):
i+=1
print j
print(j)

View File

@ -2,6 +2,7 @@
Problem Statement:
Work out the first ten digits of the sum of the N 50-digit numbers.
'''
from __future__ import print_function
n = int(input().strip())

View File

@ -1,3 +1,4 @@
from __future__ import print_function
largest_number = 0
pre_counter = 0
@ -17,4 +18,4 @@ for input1 in range(750000,1000000):
largest_number = input1
pre_counter = counter
print('Largest Number:',largest_number,'->',pre_counter,'digits')
print(('Largest Number:',largest_number,'->',pre_counter,'digits'))

View File

@ -1,3 +1,4 @@
from __future__ import print_function
# Program to find the product of a,b,c which are Pythagorean Triplet that satisfice the following:
# 1. a < b < c
# 2. a**2 + b**2 = c**2
@ -10,5 +11,5 @@ for a in range(300):
if(a < b < c):
if((a**2) + (b**2) == (c**2)):
if((a+b+c) == 1000):
print("Product of",a,"*",b,"*",c,"=",(a*b*c))
print(("Product of",a,"*",b,"*",c,"=",(a*b*c)))
break