Home > Software engineering >  "super" number in python
"super" number in python

Time:11-03

I have a problem with the task. The task is:

We say that number 1 is a super number. If a number x is super, then the numbers 2x and 3x are also super. For example, since the number 1 is super, then the numbers 2 and 3 are super. As 2 and 3 are super, then the numbers 4, 6 and 9 are super, and so on. At the same time, numbers 10 and 7 are not super. Write a program that asks the user to enter a natural number n. The program prints whether the entered number is super.

And this is what I have done so far

num = int(input("Enter a natural number "))
if num <= 0:
    print("That is not a natural number")
else:
    if num % 5 == 0 or num % 7 == 0 or num % 11 == 0 or num % 13 == 0:
        print("Number is not super.")
    elif num == 1 or num % 2 == 0 or num % 3 == 0 or num % 8 == 0 or num % 9 == 0:
        print("Number is super")
    else:
        print("Number is not super.")

this is how pydramid of numbers looks like

The problem is that for some numbers like 62 it says that it is a super number, but it ain't..

CodePudding user response:

To me it looks like you jumped in without first figuring out how you'd work it out manually.

Personally, I think the easiest way to start this would be recursively, though it'll be very inefficient with large numbers, so it's up to you if you then want to go and optimise it after doing the first version. I got it working pretty easily, but since it's a problem you need to solve, I'm not going to just copy and paste the answer.

It works something like this (psuedocode):

def is_super(i):
    if i is below 1: not a super
    if i is 1: is a super
    if i is above 1: check if i/2 or i/3 is a super

CodePudding user response:

num = int(input("Enter a natural number "))
if num <= 0:
    print("That is not a natural number")
else:
    if num % 3 == 0:
        while num % 3 == 0:
            num = num / 3
        print(num)
        if num == 1 or num % 2 == 0 or num % 3 == 0:
            print("Number is super")
        else:
            print("Number is not super")
    elif num % 2 == 0:
        while num % 2 == 0:
            num = num / 2
        print(num)
        if num == 1 or num % 2 == 0 or num % 3 == 0:
            print("Number is super")
        else:
            print("Number is not super")
    else:
        print("Number is not super")

This is what I've done so far but I don't think it will work for large numbers ?

  • Related