DEV Community

Keerthana M
Keerthana M

Posted on

Python looping [tasks]

Prime Numbers

no = 1319
div = 2
while div < no: 
    if no % div == 0:   
        print('Prime Number')
        break
    div+=1
else:
    print('Prime Number')

Enter fullscreen mode Exit fullscreen mode

Output:
Prime Number

Not A Prime Number:

no = 21
div = 2
while div < no: 
    if no % div == 0:  
        print('Not A Prime Number')
        break
    div+=1
else:
    print('Prime Number')
Enter fullscreen mode Exit fullscreen mode

Output:

Not A Prime Number

A number is a perfect number if the sum of its factors other than the given
number gives the same number. For example, 6 is a perfect number, since
adding the factors of 6 (other than 6), namely 1, 2 and 3 gives the given number 6.
i.e.,1+2+3 = 6 is the given number.
Check whether 28, 54 and 496 are perfect numbers or not.

no = 6
total = 0
factor = 1
while factor < no:
    if no % factor == 0:
        total = total + factor
    factor+=1
else:
    print(total)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
6

num =6
i=1
total=0
while i<6:
    if(num%i==0):
       total=total+i

    i=i+1

if(num==total):
    print(i," perfect number")
else:
    print(i," Not perfect")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

6 perfect number

num =28
i=1
total=0
while i<28:
    if(num%i==0):
       total=total+i

    i=i+1

if(num==total):
    print(i," perfect number")
else:
    print(i," Not perfect")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

28 perfect number

num =54
i=1
total=0
while i<54:
    if(num%i==0):
       total=total+i

    i=i+1

if(num==total):
    print(i," perfect number")
else:
    print(i," Not perfect")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

54 Not perfect

num =496
i=1
total=0
while i<496:
    if(num%i==0):
       total=total+i

    i=i+1

if(num==total):
    print(i," perfect number")
else:
    print(i," Not perfect")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

496 perfect number

2.Express 42 and 100 as the sum of two consecutive primes.

1) Divisible by 2:
Take Last Digit. It should be 0,2,4,6 or 8.

  • 1219827198271927912870
  • Last Digit: 0 2 4 6 8
no = 1290998
last_digit = no%10 
if last_digit in (0,2,4,6,8):
    print('Divisible by 2')
Enter fullscreen mode Exit fullscreen mode

OUTPUT:

Divisible by 2

2) Divisible by 3

no = 15
ones_place = no % 10  #5
tens_place = no // 10 #1
sum = ones_place + tens_place
if sum%3 ==0:
    print("Divisible by 3")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Divisible by 3

  • The sum of any three consecutive numbers is divisible by 3.
  • 33+34+35 = 102

Divisible by 4:

no = 124
last_two_digits = no % 100
if last_two_digits % 4 == 0:
    print("divisible by 4")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Divisible by 4

Divisible by 5:

no = 125
last_digit = no % 10
if last_digit == 0 or last_digit == 5:
        if last_digit in (0,5):
                print('Divisible by 5')

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Divisible by 5

Divisible by 6: 2 and 3

-12 is divisible by 2 and 3.

Divisible by 8:

  • Last 3 digits:
no =  9000
last_3_digits = no % 1000
if last_3_digits % 8 == 0:
    print("Divisible by 8")
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Divisible by 8

Divisibility by 9: by 3:

Top comments (0)