Check the highest prime number - using while and if only

Asked

Viewed 2,590 times

1

I’m having trouble solving an exercise in logic, I think I’m thinking wrong:

What I need: Given a number N show the largest prime number up to N.

The code I’ve made so far is this:

def maior_primo(n):
    print(ePrimo(n))


def ePrimo(k):
    aux = k
    i = 1
    divisores = 0
    while(aux >= 1):
        while(i <= aux):
            if(aux % i == 0):
                divisores += 1
                i += 1
            else:
                i += 1
        if(divisores < 3):
            return aux
        else:
            aux -= 1

But when I execute returns me:

maior_primo(7)
7

maior_primo(8)
None

I did both whiles because 1 is checking the number I reported and reducing ex: maior_primo(8), will test the 8 see that it has more than two splitters then will take the number I passed and subtract 1.

And the other while will check for each number that is decreasing how many dividers it has.

Note: I can only use two functions, while and if.

  • Do you want to show the largest or the smallest? If it is the smallest is easy, print(2).

  • I understood what you said, I thought wrong when I wrote it is the largest number even

  • Only one left while decrease of n until I find a cousin calling ehPrimo. I could also use the sieve to pre-populate the cousins

  • @Jeffersonquesado, so it’s in the code this while

  • Not in the ehPrimo, but calling (present continuous/gerund) ehPrimo. That is, inside of maior_primo. And the call of maior_primo outside the print

  • 2

    Its function ePrimo is not checking whether a number is prime or not, is trying to solve the problem of maior_primo. Instead, make her return True if a number is prime and False otherwise. Having it working, then yes you will do the maior_primo.

Show 1 more comment

1 answer

3


Its function ePrimo is not checking whether a number is prime or not, is trying to solve the problem of maior_primo. Instead, make her return True if a number is prime and False otherwise. Having it working, then yes you will do the maior_primo.

Another detail to consider is that if k is a number is composed, so the biggest factor it can have is its square root. You don’t need to calculate the square root directly, just check that for any possible splitter i, if i2 <= k.

Here’s what the code looks like (including testing):

def maior_primo(n):
    aux = n
    while aux > 2:
        if eh_primo(aux):
            return aux
        aux -= 1
    return 2

def eh_primo(k):
    i = 2
    while i * i <= k:
        if k % i == 0:
            return False
        i += 1
    return True

print('Maior primo ate 8: ' + str(maior_primo(8)))
print('Maior primo ate 7: ' + str(maior_primo(7)))
print('Maior primo ate 100: ' + str(maior_primo(100)))
print('Maior primo ate 60: ' + str(maior_primo(60)))
print('Maior primo ate 61: ' + str(maior_primo(61)))
print('Maior primo ate 3: ' + str(maior_primo(3)))
print('Maior primo ate 2: ' + str(maior_primo(2)))
print('Maior primo ate 1: ' + str(maior_primo(1)))
print('Maior primo ate 0: ' + str(maior_primo(0)))

Here is the output produced:

Maior primo ate 8: 7
Maior primo ate 7: 7
Maior primo ate 100: 97
Maior primo ate 60: 59
Maior primo ate 61: 61
Maior primo ate 3: 3
Maior primo ate 2: 2
Maior primo ate 1: 2
Maior primo ate 0: 2

See here working on ideone.

  • Thanks for the explanation and example, congratulations! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.