Archive
Python: Collatz Sequence
Python: Longest Collatz Sequence
Problem No.14 in ProjectEuler
Definition Wikipedia: Start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.
So the formula is: Starting with n, the next n will be:
n/2 (if n is even)
3n + 1 (if n is odd)
If we start with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1.
A walk through:
n=13, 13 is odd, then n = 13 * 3 + 1 , n= 40
n=40, 40 is even, then n= 40/2, n=20
n=20, 20 is even, then n=20/2, n=10
n=10, 10 is even, then n=10/2, n=5
n=5, 5 is odd, then n=5*3+1 , n=16
n=16, 16 is even, then n=16/2, n=8
n=8, 8 is even, then n=8/2, n=4
n=4, 4 is even, then n=4/2, n=2
n=2, 2 is even, then n=2/2, n=1
n=1 then end of sequence
The Task: The task in ProjectEuler is to searching for a the Number N, under one million, that produces the longest chain.
Overview to my python cases: In my company, we are not allowed to download any software, so i don’t have any Python platform. To solve this i am using an online python interpreter, some time it’s become slow. So in this code (and others) i am spiting the range in 10 each with 100,000 then running the code to get the longest chain in each range. So the Number N, under one million, that produces the longest chain is:
The Answer: In my previous codes or math solving challenges in pybites or ProjectEuler I am solving the problems, writing the code, but not posting my answer to ProjectEuler platform. Today, and with Problem No.14 i decide to post the answer in the ProjectEuler platform for the first time just to see what will happen. The answer was 837799, and I get this page.
In the code bellow, i set the range from 1 to 50000.
The Code:
chain2=[]
longest=[0,0]
def collatz_Seq(num):
t= num
chain=[num]
while t !=1 :
if t%2==0:
t=t/2
chain.append(int(t))
else:
t=3*t+1
chain.append(int(t))
return chain
for num in range (1,50000):
chain2 = collatz_Seq(num)
if len(chain2) > longest[0]:
longest[0] = len(chain2)
longest[1] = num
chain2=[]
print(‘num:’,longest[1],’ has a longest chain: ‘,longest[0])
Python: The Factors
Python: Factors of the Number N
This is a short task to get the factors of a given number. The Definition of Factors of N is: The pairs of numbers you multiply to get the N number.
For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4
In this task we will write a Python code to ask the user for a number N then will get all the pairs number that if we multiply them will get that N number, we will store the pairs in a array ‘factors’.
The Code:
def factors_of_n(num):
a=1
factors=[]
while a <= num:
if num%a==0:
if (num/a,a) not in factors:
factors.append((a,int(num/a)))
a = a + 1
return factors
#Ask the user for a number
num=int(input(“Enter a number: “))
print(factors_of_n(num))
Python: Amicable Numbers
Python: Amicable Numbers
Problem No.21 on projectEuler
In this task we need to calculate the SUM of all divisors of N, from 1 to N.
Then we calculate the Sum of (sum of N divisors ) let’s say M .
Now if
For example, the proper divisors of A=220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore F(220) = 284. And the proper divisors of B=284 are 1, 2, 4, 71 and 142; so F(284) = 220. So F(A) =B and F(B)=A then A and B are amicable.
Definition:
If f(a) = b and f(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
In this task we will ask the user to enter a Range of numbers and we will search for all Amicable Numbers pairs in that range(from – to) if we fond one we will print out the number and the divisors list. The main function here is the one that get the sum of divisors, we will call it get_divisors_sum and we will examine Amicable with If statement.
Hint ..
To check if the sum of divisors from both said are equal we use (t2==x )
AND that this sum are not same we use (t1!=t2)
AND that we did not print the pair before we use: (t2 not in ami_pair)
The Code:
#Python: Amicable Numbers
#Problem No.21 on projectEuler
num1=int(input(‘The range Start from:’))
num2=int(input(‘The range Ends at:’))
t1=0
t2=0
l=[]
l1=[]
l2=[]
ami_pair=[]
def get_divisors_sum (num):
t=0
l=[]
for a in range (1,num):
if num%a==0 :
l.append(a)
t=t+a
return t,l
for x in range(num1,num2):
l1=[]
t1,l1=get_divisors_sum(x)
l2=[]
t2,l2=get_divisors_sum(t1)
if (t2==x) and (t1!=t2) and (t2 not in ami_pair):
print(‘\nget_divisors_sum({}) is {} divisors={}’.format(x,t1,l1))
print(‘\nget_divisors_sum({}) is {} divisors={}’.format(t1,t2,l2))
print(‘\nSo {} and {} are Amicable Numbers .’.format(t1,t2))
ami_pair.extend((t1,t2))
Python Project: Sum of power of digits
Python: Sum of power of digits
Problem No.16
In Problem No.16, projectEuler ask to find the Sum of power of digits in the number, for example if we have 2^15 (2 to power of 15) the answer is 23768 then we need to calculate the sum of this number (2+3+7+6+8 ) that’s equal to 26.
The Task: So our task in this project is to find the sum of the digits of (2^1000). To write this as a program and to make it more general we will ask the user to input the number and the power he want, then I start thinking to restrict user from input large numbers that could cause CPU problems, but then i decide to keep it open as is.
The Code:
num=2
p=15
num=int(input(“Enter a number “))
p=int(input(“Enter a power “))
a=num**p
def sum_of_digits(num,p):
a=num**p
l=[int(i) for i in str(a)]
print(l)
for x in range (len(l)):
t=t+l[x]
print (“sum =”,t)
Python : Expenditure App
Expenditure Application
The Menu:
Continuing parts of the Expenditure Application, here is the basic menu that we will use in our app. We will select (9) as Exit from the system.
So now, we have a json file with sample data and stored in a directory, also we have a file called “expen.py” and we will write the menu def in it and save it.
def the_menu(): # This is a menu
print(‘1. Add New Entry.’)
print(‘2. Delete an Entry.’)
print(‘3. Change an Entry.’)
print(‘4. Show Total Expenditure in a Year.’)
print(‘5. Show Expenditure Details in a year.’)
print(‘9. Exit.’)
# This line will ask the user to enter the operation he want to do.
choice = input(‘What you want to do? ‘)
return choice
In this app we will have 5 functions as our menu and maybe one or two functions to facilitate some calculations in background.
Python Project: Expenditure App Part 1
Python Project:
Two months back I start to learn #Python “Python is a #computer #programming #language “, you can say it grabbed me very quickly. I start to read some #sample #code from the net, then downloading the requirement and after 10 working days I was writing #codes in python and doing what I love ‘Solving #mathematics puzzles with #python’.
So I decide to contribute in training or publishing python language by posting some codes block with explaining and comments, also by developing an application so we will learn on a real example.
I am publishing some codes on my Twitter (@h_ta3kees):
https://twitter.com/h_ta3kees?s=09
and I will do the same here on my blog.
The coming posts will be about the application that we will start develop I will call it ‘Expenditure App’.
Photos from Qatar, Al-Khawr City
Photos from Qatar, Al-Khawr City 2009
A picture of a sunset in Al-Khawr City in 2009 using Nikon D90 and Nikkor 18-105mm camera in hand.

Location: Al-Khawr City, Qatar.
20-9-2009, 5:34AM, GMT + 3
On Google Earth:
N 25 41′ 10.89″
E 51 30′ 35.17″
Photos from Qatar : The Museum of Islamic Art
Photos from Qatar: The Museum of Islamic Art
A picture of The Museum of Islamic Art in Qatar, photo taken from airplane window in 2012 using Sone T100.
I take this shot with Sony T100 camera on 15/9/2012, the plane just takeoff, camera in hand.
Click here to see some photos from the Qatar Islamic Art Museum Collections
صور من قطر:صوره جوية للمتحف الاسلامي في قطر، تم التقاط الصورة من خلال شباك الطائره خلال الاقلاع سنة 2012.
صور من قطر:صوره جوية للمتحف الاسلامي في قطر، تم التقاط الصورة من خلال شباك الطائره خلال الاقلاع سنة 2012.
Photos From Qatar : Al-Wakra 3
Photos from Qatar, Al-Wakra
A picture of Al Wakra Market “Souq Waqif” it is a traditional old-fashioned market Built in the old style of country houses. It is located close to the sea coast in Al Wakrah area, south of Qatar.
Using Nikon S9900 camera, camera in hand
صور من قطر: الوكره
هذه صوره لسوق الوكره “سوق واقف” سوق مبني على الطراز القديم التراثي للمنازل القطريه القديمه والسكك، وهو يقع قريب من ساحل البحر في منطقه الوكره جنوب قطر.
صور من قطر: الوكره
هذه صوره لسوق الوكره “سوق واقف” سوق مبني على الطراز القديم التراثي للمنازل القطريه القديمه والسكك، وهو يقع قريب من ساحل البحر في منطقه الوكره جنوب قطر.
My Sketchbook
I am sketching to enhance my skill, here is one of sketches from my sketchbook using pencil as a guide-line and ink-pen to finish the drawing.
Click to Enlarge
::My SketchBook::
Sketch of: Rhinoceros
Using: Ink-Pen
Date: 1/8/2017
SketchBook: SketchBook # 20
More Sketches: Click Here
Ali,


Follow me on Twitter..







