Merge branch 'master' into modernize-python2-code

This commit is contained in:
cclauss
2017-12-13 16:32:28 +01:00
committed by GitHub
18 changed files with 1053 additions and 173 deletions

View File

@ -13,8 +13,8 @@ try:
except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
sum=0;
num=0;
sum=0
num=0
while(1):
num+=3
if(num>=n):
@ -44,4 +44,5 @@ while(1):
if(num>=n):
break
sum+=num
print(sum);

View File

@ -14,7 +14,9 @@ except NameError:
raw_input = input # Python 3
n = int(raw_input().strip())
i=1; j=2; sum=0
i=1
j=2
sum=0
while(j<=n):
if((j&1)==0): #can also use (j%2==0)
sum+=j

View File

@ -18,7 +18,7 @@ def isprime(no):
return False
return True
max=0
maxNumber = 0
n=int(input())
if(isprime(n)):
print(n)
@ -32,8 +32,8 @@ else:
for i in range(3,n1,2):
if(n%i==0):
if(isprime(n/i)):
max=n/i
maxNumber = n/i
break
elif(isprime(i)):
max=i
print(max)
maxNumber = i
print(maxNumber)

View File

@ -4,13 +4,26 @@ A palindromic number reads the same both ways. The largest palindrome made from
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)
if(temp==temp[::-1]):
j=999
while(j!=99):
if((i%j==0) and (len(str(i/j))==3)):
print(i)
limit = int(input("limit? "))
# fetchs the next number
for number in range(limit-1,10000,-1):
# converts number into string.
strNumber = str(number)
# checks whether 'strNumber' is a palindrome.
if(strNumber == strNumber[::-1]):
divisor = 999
# if 'number' is a product of two 3-digit numbers
# then number is the answer otherwise fetch next number.
while(divisor != 99):
if((number % divisor == 0) and (len(str(number / divisor)) == 3)):
print(number)
exit(0)
j-=1
divisor -=1

View File

@ -0,0 +1,16 @@
# 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?
def isprime(number):
for i in range(2,int(number**0.5)+1):
if number%i==0:
return False
return True
n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted
primes = []
num = 2
while len(primes) < n:
if isprime(num):
primes.append(num)
num += 1
else:
num += 1
print(primes[len(primes) - 1])

View File

@ -0,0 +1,15 @@
power = int(input("Enter the power of 2: "))
num = 2**power
string_num = str(num)
list_num = list(string_num)
sum_of_num = 0
print("2 ^",power,"=",num)
for i in list_num:
sum_of_num += int(i)
print("Sum of the digits are:",sum_of_num)

View File

@ -0,0 +1,27 @@
# Finding the factorial.
def factorial(n):
fact = 1
for i in range(1,n+1):
fact *= i
return fact
# Spliting the digits and adding it.
def split_and_add(number):
sum_of_digits = 0
while(number>0):
last_digit = number % 10
sum_of_digits += last_digit
number = int(number/10) # Removing the last_digit from the given number.
return sum_of_digits
# Taking the user input.
number = int(input("Enter the Number: "))
# Assigning the factorial from the factorial function.
factorial = factorial(number)
# Spliting and adding the factorial into answer.
answer = split_and_add(factorial)
# Printing the answer.
print(answer)

View File

@ -0,0 +1,34 @@
def main():
"""
Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5:
22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by ab for 2 <= a <= 100 and 2 <= b <= 100?
"""
collectPowers = set()
currentPow = 0
N = 101 # maximum limit
for a in range(2,N):
for b in range (2,N):
currentPow = a**b # calculates the current power
collectPowers.add(currentPow) # adds the result to the set
print "Number of terms ", len(collectPowers)
if __name__ == '__main__':
main()

View File

@ -49,3 +49,10 @@ PROBLEMS:
Using the rule above and starting with 13, we generate the following sequence:
13 40 20 10 5 16 8 4 2 1
Which starting number, under one million, produces the longest chain?
16. 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?
20. n! means n × (n 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!