Posts Tagged ‘radwani’

Python: Multiples of Numbers

April 29, 2019 Leave a comment

Python: Multiples of 3 and 5
Problem No.1 in ProjectEuler

This is very easy, very short task to work on, the task as is in ProjectEuler like this “Find the sum of all the multiples of 3 or 5 below 1000.”

My way, as i like to do open code works for any numbers, we will ask the user to enter three numbers, num1 and num2 will be as (3 and 5) in the task, my_range will be as the 1000. So the code can get the sum Multiples of any two numbers in a ranges from 1 to my_range.

Check the answer if you like.

The Code:

# Multiples of 3 and 5
# ProjectEuler: Problem 1

def Multiples_of_N (num1,num2,my_range):


for t in range (1,my_range):

if t %num1==0 or t%num2 ==0 :

tot = tot + t

return tot

print ‘\nDescription: This function will take three variables, two numbers represint the what we want to get there Multiples, then we ask for a range so we will start from 1 to your range.\n’
num1=int(input(‘Enter the first number:’))
num2=int(input(‘Enter the second number:’))
my_range =int(input(‘Enter the range (1, ??):’))

total=Multiples_of_N (num1,num2,my_range)

print ‘\nYou entered ‘,num1,’,’, num2,’ So the sum of all multiples of those number in range (1-‘,my_range,’) = ‘,total

Follow me on Twitter..


Python: Factorial Digit Sum

April 25, 2019 Leave a comment

Python: Factorial Digit Sum
Problem 20 @ projectEuler

The Task: The task in projectEuler P20 is to get the sum of the digits in the number Factorial of 100!

Factorial NdefinitionThe factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800.

Problem 20 is another easy problem in projectEuler, and we will write two functions to solve it. First one is a Factorial_digit_sum this one will return the factorial of a number. The second function will calculate the sum of all digits in a number N and we will call it sum_of_digits.

Clarification As long as i just start solving or posting my answers to projectEuler portal, i am selecting problems and not going through them in sequence, that’s way my posts are jumps between problems, so if i am posting the code to solve problem 144 (for example) that does’t meaning that i solve all problems before it.

print of solved screen:

The Code:

#Python: Factorial Digit Sum
#Problem No.20 on projectEuler

def Factorial_digit_sum(num):

if (num == 1) :

return 1


return num * Factorial_digit_sum(num-1)

fact =Factorial_digit_sum(100)
print fact,’is the Factorial of {}.’.format(num)

def sum_of_digits(dig):

t = 0

for each in dig:

t = t + int(each)

print ‘\nThe sum of your number is’,t


Follow me on Twitter..

Python Project

December 12, 2018 Leave a comment

Python: Armstrong Numbers

Check for Armstrong number in a range

In our last post (Check if a Number is Armstrong) we wrote a codes to check a given number and see whether it is an Armstrong or Not.

Today, we want to go through a set of numbers and see how many Armstrong numbers are there.

Before calling the ‘armstrong_in_range()’ and just to keep the code as less as we can, I assume the two numbers has the same number of digits, also I am getting the power (p) and the range numbers (n1, n2) out-side of this function and passing all as variables.

def armstrong_in_range(p,n1,n2):

my_sum = 0

count = 0

for num in range (n1,n2):


for arm in range(p):

my_sum = my_sum + ((num % 10)**p)

num = num // 10

if my_sum == tot:

print(‘\nThe number {} is Armstrong.’.format(tot))


count = count +1



print(‘\nWe have {} armstrong number(s)’.format(count))

Follow me on Twitter..

Python Project

December 9, 2018 1 comment

Python: Armstrong Numbers

Check if a Number is Armstrong

In late nineties, I was programming using Pascal Language and I was very passionate to convert some mathematical syntax into codes to fine the result, although some of them were very easy; but the goal was to write the codes.

Today, we are attempted to write a code in Python to check whether a number is an Armstrong or Not. First let’s ask:

what is Armstrong number?
Answer: If we assume we have a number (num = 371), 371 is an Armstrong because the sum of each digits to the power of (number of the digits) will be the same. That’s mean 371 is a three digits so the power (p=3) so:

3**3 = 27
7**3 = 343
1**3 = 1

then (27+343+1) = 371. … So 371 is an Armstrong Number.

In wikipedia:

Armstrong also known as a pluperfect digital invariant (PPDI) or the Narcissistic number is a number that: the sum of its own digits each raised to the power of the number of digits equal to the number its self.

# Function to check whether a number is Armstrong or Not.

def is_it_armstrong(num):

p= len(str(num)) # First: we get the power of the number



for x in range (p) :



if my_sum == tot:

print(‘\nThe number {} is Armstrong.’.format(tot))

else :

print(‘\nThe number {} is NOT Armstrong.’.format(tot))

Follow me on Twitter..

Python Project

December 6, 2018 Leave a comment

Python: Draw Flower

Drawing with Python

In a previous post, we use the t.forward(size) & t.left(x) to draw something looks-like flower petal. That post (See it Here) is drawing (8) petals facing left side to complete a flower shape… So can we improve it? Can we make it looks better?..

OK, I work on it and divide the code into two functions, one will draw a petal facing left side and another to draw a petal facing right side, each petals will be close to each other and touching or almost touching each’s head. Bellow I am posting the codes, and an image for the result.

You can play with the code and change the numbers, size and rotation degree and let’s see what we will have.

#To draw a flower using turtel and circle function
import turtle
# Here is some setting for the turtel
t = turtle.Turtle()

#Draw petal facing left side
def petal_l():


for x in range (40):




#Draw petal facing right side
def petal_r():


for x in range (40):





# To draw 5 petals
for pet in range (5):

petal_l ()



petal_r ()



Follow me on Twitter..

The Code: The Result

Python Drawing flower

December 5, 2018 Leave a comment

Python: Draw Flower

Drawing with Python

In this post I am using some codes to draw mathematical shapes that looks like flower.

To do this we need to import turtle library, then using circle function to complete our work. You may play around with the numbers and figure out what will happen.

#python #code
import turtle
t = turtle.Turtle()
for x in range (30):,70)

if x % 2 == 0:,70)

Follow me on Twitter..

Python : Expenditure App

December 4, 2018 Leave a comment

Expenditure Application

Add new Entry

Today we will work on the “Add New Entry” function, this is first function on our menu. (Read Expenditure App Menu here)

We will call the function def add_entry(): and will give the user the prompt to enter “Date” and “Amount” then we will write them to our json file.

Just to be as less codes as we can, I am not adding any validations here also not coding the try .. except. So I am assuming the entered data will be in correct way.

def add_entry():

my_date = input(‘Enter the date as dd/mm/yyyy: ‘)

my_amount = input(‘Enter the Amount: ‘)

new_data = {“date”: my_date, “amount”: int(my_amount)}

# Here we adding the new data to the file

ff = open(“expen_dat.json”, “a”), 2) # goto the end of the file.

ff.truncate(ff.tell() – 3) # from end of the file erase last 3 char.





If you add the “User input choice” in your code this function will be on choice “1” as here.

if choice == ‘1’:


choice = the_menu()
if choice == ‘2’:

Follow me on Twitter..

Table of posts regarding the Expenditure App..

Title Description Post Link
The Case Talking about the case study of the application Click to Read
leveling the Ground Talking about: Requirement, folder structure, data file json, sample data and .py file and other assumptions Click to Read
The Menu Talking about: The application menu function Click to Read
User input ‘choice’: Talking about: The user input and the if statement to run the desired function Click to Read
Loading and testing the Data Talking about: Loading the data from json file and print it on the screen. Click to Read
Add New Entry Talking about: The “Add New Entry” function

Here is the code shot..

Here is the output screen..