Sunday, February 26, 2023
HomeSoftware EngineeringCalculating Odd/Even variety of divisors in Python

Calculating Odd/Even variety of divisors in Python

The problem

Given an integer n return "odd" if the variety of its divisors is odd. In any other case, return "even".

Be aware: massive inputs might be examined.


All prime numbers have precisely two divisors (therefore "even").

For n = 12 the divisors are [1, 2, 3, 4, 6, 12] – "even".

For n = 4 the divisors are [1, 2, 4] – "odd".

The answer in Python code

Possibility 1:

def oddity(n):
    #your code right here
    return 'odd' if n**0.5 == int(n**0.5) else 'even'

Possibility 2:

import math
def oddity(n):
    return math.sqrt(n) % 1 == 0 and 'odd' or 'even'

Possibility 3:

oddity=lambda n: ["odd","even"][n**.5%1!=0]

Take a look at instances to validate our answer

import check
from answer import oddity

@check.describe("Pattern exams")
def exams():"Some examples")
    def exams():
        check.assert_equals(oddity(1), 'odd')
        check.assert_equals(oddity(5), 'even')
        check.assert_equals(oddity(16), 'odd')


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto