Ad

All programs should be one line long!
Long live the Magical Python One-Liner!!

Code
Diff
  • basicOp=lambda o,v,w:v/w if o=='/'else{'+':v+w,'-':v-w,'*':v*w}[o]if o in'+-*'else"Invalid Operation"
    # Python one-liners are turing-complete, so why would you ever use more than that?
    • def basicOp(operation, value1, value2):
    • if operation == "+":
    • return value1 + value2
    • if operation == "-":
    • return value1 - value2
    • if operation == "*":
    • return value1 * value2
    • if operation == "/":
    • return value1 / value2
    • return "Invalid Operation"
    • basicOp=lambda o,v,w:v/w if o=='/'else{'+':v+w,'-':v-w,'*':v*w}[o]if o in'+-*'else"Invalid Operation"
    • # Python one-liners are turing-complete, so why would you ever use more than that?
Code
Diff
  • def factorise(a): # takes much less than O(a), unless a is prime
        prime_factors = {}
        p = 1
        while a > 1:
            p += 1
            while a % p == 0:
                if p in prime_factors: prime_factors[p] += 1
                else: prime_factors[p] = 1
                a = a//p
        return prime_factors
    
    def totient(a):
        """python 3.6.0"""
        primes = factorise(a)
        totient = 1
        for i in primes: # formula for totient from prime factorisation
            totient *= i**(primes[i]-1) * (i-1)
        return totient
    • from math import gcd
    • def factorise(a): # takes much less than O(a), unless a is prime
    • prime_factors = {}
    • p = 1
    • while a > 1:
    • p += 1
    • while a % p == 0:
    • if p in prime_factors: prime_factors[p] += 1
    • else: prime_factors[p] = 1
    • a = a//p
    • return prime_factors
    • def totient(a):
    • """python 3.6.0"""
    • return len([b for b in range(a) if (gcd(a, b) == 1)])
    • primes = factorise(a)
    • totient = 1
    • for i in primes: # formula for totient from prime factorisation
    • totient *= i**(primes[i]-1) * (i-1)
    • return totient