Wednesday, June 22, 2016

An Anagram Detection Example



  • One string is an anagram of another if the second is simply a rearrangement of the first.
  • We check to see that each character in the first string actually occurs in the second. 



Python code:

def anagramSolution1(s1,s2):

s1= s1.replace(" ", "").lower()
s2= s2.replace(" ", "").lower()

#edge cases
if len(s1) != len(s2):
return False
# create a dictonary
count = {}

for letter in s1:
if letter in count:
count[letter] += 1

else:
count[letter] = 1

for letter in s2:
if letter in count:
count[letter] -= 1
else:
count[letter] = 1

for k in count:
if count[k] != 0:
return False

return True

print(anagramSolution1('abcd','dcba'))

Wednesday, June 15, 2016

Day 3: 10001st prime


Question:
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 10 001st prime number?


def isPrime(n):    
    if n < 2: return "Neither prime, nor composite"
    for i in range(2, n):       
    # even division so its false      
         if n % i == 0:            
            return False    
    return True

def nthPrime(n):   
    numberOfPrimes = 0    
    prime = 1
    while numberOfPrimes < n:       
          prime += 1        
          if isPrime(prime):            
              numberOfPrimes += 1    
    return prime

print(nthPrime(10001))












Picture:






Tuesday, June 14, 2016

Day 2: Sum square difference

Question:

The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
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 one hundred natural numbers and the square of the sum.


Solution:

The sum of natural numbers can be expressed as
\displaystyle S_n = \frac{n(n 1)}{2}


The square of the sum:                                                                                                
\displaystyle S_n^2 = \frac{n(n 1)(2n 1)}{6}

Python code:

product =0
for i in range(1,101):    
    square = i* i #suare of sum    #sum of square    
    product +=square
print("The sum of the squares of the first ten natural numbers is,",product)

# The square of the sum of the first ten natural numbers is,
num =0
for i in range(1,101):   
    num += i
# squaring the number
result = (num)*(num)

print("The square of the sum of the first ten natural numbers i",result)
print ("The difference between the sum of the squares of the first ten 
        \natural numbers and the square of the sum is",result - product)


Monday, June 13, 2016

Day 1: largest palindrome

Question 1)

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.



Answer:

The largest palindrome can be written as: abccba Which then simpifies to: 100000a + 10000b + 1000c + 100c + 10b + a And then: 100001a + 10010b + 1100c Factoring out 11, you get: 11(9091a + 910b + 100c) So the palindrome must be divisible by 11. Seeing as 11 is prime, at least one of the numbers must be divisible by 11.


#Create a sequence from 100 to 999
three_dig = range(100,1000)
# and an empty list for the palindromes
list_palindromes = []
# loop twice over the number sequence
for n1 in three_dig:    
for n2 in three_dig:       
 # Get the product       
 number =  n1 * n2
 # Transform it in string      
 number = str(number)
 # Check the whole string to test      
  # if it's a palindrome       
 if number == number[::-1]:         
   # Add the palindrome to the list        
 list_palindromes.append(int(number))
print max(list_palindromes)