DEV Community

Durga Pokharel
Durga Pokharel

Posted on • Edited on

2

Day 12 of 100DaysOfCode: Python Program to make Non Perfect Number Perfect

This is my 12th day of #100daysodcode. Today I worked more about CSS properties on freecodecamp. Did some assignments. And tried to write python program to make non perfect number perfect. Below is mine code of the day.

Program to make Non Perfect Number Perfect

In this code at first users give integer type number. After that I define the root if square of root is given number then our given number is already perfect. If there is no this possibility we go further. Initialize before after and curr-number as given. We have no idea how long our loop going so I took while loop.

I defined croot below. If given number less than current number at that case we take before number is equals to current number. If given number greater than current number at that time our after number is equal to current number.

n = int(input("enter an integer"))
root = int(n**0.5)

if root**2 == n:
    print(f'your number {n} is already perfect.')
else:

    # find the perfect numer before and after the  number.
    before = 0
    after = 1
    curr_num = 1

    while True:

        croot = int(curr_num**0.5)

        if croot**2 == curr_num:
            if curr_num < n:
                before = curr_num
            else:
                after = curr_num
        #print(curr_num,before,after)

        if after != 1:
            if n-before < after-n:
                print(f'perfect numbers around {n} are {before} and {after}.')
                print(f'your nearest perfect number of {n} is {before}')
                print(f'we have to subtract {n-before} to make {n} perfect number.')
                break
            else:
                print(f'perfect numbers around {n} are {before} and {after}.')
                print(f'your nearest perfect number of {n} is {after}')
                print(f'we have to add {after-n} to make {n} perfect number.')
                break
        curr_num += 1

Enter fullscreen mode Exit fullscreen mode

12 Day of #100daysofcode and python
* More about CSS properties
* Tried to write python program to make non perfect number perfect.#100daysofcode, #codenewbie, #beginner ,#Python pic.twitter.com/9ButCsWhZ7

— Durga Pokharel (@mathdurga) January 5, 2021

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (2)

Collapse
 
kingsleyijomah profile image
Kingsley Ijomah

well done! good work :)

Collapse
 
iamdurga profile image
Durga Pokharel

Thank you

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay