DEV Community

Sowntharya
Sowntharya

Posted on

Python problems and solutions

Problem: 1
A book seller has 175 english books, 245 science books and 385 mathematics books. He wants to sell the books in a box, subject-wise in equal number. What will be the greatest number of the boxes required.

To Find Common Factors of 175, 245, and 385

Problem: 2
What is the smallest 5 digit number that is exactly divisible by 72 and 108?

  • smallest 5digit number is 10000.
  • largest 5digit number is 99999.
  • We need to find a number that is divisible by both 72 and 108.
  • We need the smallest number.
no = 10000
while no <= 99999:
    if no%72==0 and no%108==0:
        print(no)       //10152
        break
    no+=1
Enter fullscreen mode Exit fullscreen mode

note:
no = 10000 – Starts from the smallest 5-digit number.
while no <= 99999 – Checks all 5-digit numbers.
no % 72 == 0 – Checks divisibility by 72.
no % 108 == 0 – Checks divisibility by 108.
and – Both conditions must be true.
print(no) – Displays the answer.
break – Stops the loop after finding the smallest number.
no += 1 – Increases the number by 1.

Problem: 3
There are four Mobile Phones in a house. At 5 a.m, all the four Mobile Phones will ring together. Thereafter, the first one rings every 15 minutes, the second one rings every 20 minutes, the third one rings every 25 minutes and the fourth one rings every 30 minutes. At what time, will the four Mobile Phones ring together again?

Find the prime factors.
15 = 3 × 5
20 = 2² × 5
25 = 5²
30 = 2 × 3 × 5

Find the LCM.
Take the highest powers of each prime number:
LCM=2²×3×5 ==4×3×25=300 minutes

Convert minutes into hours.
300÷60=5 hours

Add 5 hours to 5 a.m.
5:00 a.m.+5 hours=10:00 a.m

a = 15
b = 20
c = 25
d = 30
lcm = 1

while True:
    if (lcm % a == 0 and lcm % b == 0 and lcm % c == 0 and lcm % d == 0):
        break
    lcm += 1

print("LCM =", lcm, "minutes")        //300minutes

hours = lcm // 60
minutes = lcm % 60

print("All four phones ring together after", hours, "hours and", minutes, "minutes.")

Enter fullscreen mode Exit fullscreen mode

Problem: 4
Perfect Number
A perfect number is a positive integer that equals the sum of its positive proper divisors (divisors smaller than the number itself). The first perfect numbers are 6 and 28 (since 1+2+3=6 and 1+2+4+7+14=28).

6 --> its divisors are 1,2,3 --> 1+2+3 --> 6
28 --> 1+2+4+7+14=28
1) Divisors
2) Sum of Divisors

n = 6
i = 1
sum = 0

while i < n:
    if n % i == 0:
        sum = sum + i
    i = i + 1

if sum == n:
    print("Perfect Number")  //perfect number
else:
    print("Not a Perfect Number")
Enter fullscreen mode Exit fullscreen mode

note:
n = 6
i = 1
sum = 0

First round:
i = 1
Is 6 divisible by 1?
Yes, So add 1 to sum.
sum = 0 + 1 = 1

Second round:
i = 2
repeat the process

That means 6 < 6, which is False.
So the loop stops.

Problem: 5
Neon Number:
A neon number is a number where the sum of digits of its square is equal to the original number.
Eg. 9 -->
1) square => 9*9=81 # no ** 2 81
2) Sum of Digits => Add digits of square 8+1=9
3) Equal - Neon Number => Compare the sum with original number.
9 == 9 → True.

no=9
sum=0
square = no**2

while(square>0):
    sum = sum+square%10
    square = square//10  

if(sum==no):
    print("neon number")
else:
    print("not a neon number")
Enter fullscreen mode Exit fullscreen mode

note:
% 10 gets the last digit.
// 10 removes the last digit.
Always extract digits from the square when checking a Neon Number.

Problem: 6
Duck number:
A Duck Number is a number that contains at least one zero (0), but the zero should not be at the beginning of the number.

no = int(input("Enter a number: "))

temp = no
flag = 0

while(temp > 0):
    digit = temp % 10

    if(digit == 0):
        flag = 1
        break

    temp = temp // 10

if(flag == 1):
    print("Duck Number")  
else:
    print("Not a Duck Number")
Enter fullscreen mode Exit fullscreen mode

note:
temp % 10 → Gets the last digit.
temp // 10 → Removes the last digit.
flag = 1 – Indicates that zero is found.
break – Stops the loop when zero is found.

Problem: 7
Spy Number:
The number is said to be a Spy number if the sum of all the digits is equal to the product of all digits.
1) Sum of All Digits
2) Product of All Digits
3) Compare
Eg: 1412 --> 1+4+1+2 --> 8, 1*4*1*2 --> 8

no=1412
sum = 0
product = 1

while(no>0):
    sum = sum + no%10          #digit = no%10
    product = product * (no%10)
    no = no//10

if(sum == product):
    print("Its a spy number")  //Its a spy number
else:
    print("Not a spy number")
Enter fullscreen mode Exit fullscreen mode

note:
digit = no % 10 => Gets the last digit
sum = sum + digit=> Adds the digit to sum
product = product * digit=> Multiplies the digit with product
no = no // 10=> Removes the last digit
if(sum == product)=> Checks whether sum and product are equal

Problem: 8
Amicable Numbers:
Check whether 1184 and 1210 are amicable numbers.
Two numbers are said to be amicable numbers if the sum of the factors of one number (except the number itself) gives the other number.
The numbers 220 and 284 are amicable, since the sum of the factors of 220 (except 220) i.e., 1+2+4+5+10+11+20+22+44+55+110 = 284 and the sum of the factors of 284 (except 284) i.e., 1+2+ 4+71+142 = 220.

220 --> 1+2+4+5+10+11+20+22+44+55+110 = 284
284 --> 1+2+ 4+71+142 = 220

no1 =1184
n02 =1210
sum1 =0
sum2 =0
i = 1

while(i < no1):
if(no1 % i ==0):
sum1 = sum1=i
i = i + 1

while(i < no2):
if(no2 % i ==0):
sum2 = sum2=i
i = i + 1

if(sum1 == no2 and sum2 == no1):
    print("Amicable Numbers")   //Amicable Number
else:
    print("Not Amicable Numbers")
Enter fullscreen mode Exit fullscreen mode

note:
sum1 = 0 initializes the sum.
i = 1 starts checking factors from 1.
while(i < no1) checks numbers up to 1183.
no1 % i == 0 checks whether i is a factor of 1184.
sum1 = sum1 + i adds the factor to the sum.
i = i + 1 increases the value of i.

Problem: 9
Strong Number:
Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number.
1) no = 145
2) Split 5, 4 and 1
3) Write a function - def find_factorial(no): factorial = 1
4) Add results we got from Step #3.
5) given number = total --> Strong Number
1! = 1
4! = 4 × 3 × 2 × 1 = 24
5! = 5 × 4 × 3 × 2 × 1 = 120
1+24+120 = 145

[Sum of First n numbers --> Multiplication of first n numbers]

no = 145
temp = no
sum = 0

while(no > 0):
    digit = no % 10
    fact = 1
    i = 1

while(i <= digit):
    fact = fact * i
    i = i + 1
    sum = sum + fact
    no = no // 10

if(sum == temp):
    print("Strong Number")   //Strong Number
else:
    print("Not a Strong Number")
Enter fullscreen mode Exit fullscreen mode

note:
used two while loop:
Outer while loop – extracts each digit.
Inner while loop – calculates the factorial of each digit.

% 10 → Extracts the last digit
// 10 → Removes the last digit
fact = 1 → Initializes factorial
fact = fact * i → Calculates factorial
sum = sum + fact → Adds factorial values

Top comments (0)