WorksApp Interview Question for Research Scientists






Comment hidden because of low score. Click to expand.
2
of 2 vote

# A fraction p/q is irreducible and smaller equal 1 if gcd(p,q)=1 and p < q.
# Hence the number of irreducible functions of the form i/j where i < j equals phi(j)
# where phi is the Euler's totient function, that is summing phi(j) over j= 2,..,n
# (n being the input number) we get the desired result.
# First get prime numbers smaller or equal than input number n via Eratostene's sieve
# and store them in an array O(nlog(log(n))).
# For every i=m,...,n compute Euler's totient function phi(i) via the product formula using
# the array of primes to find the prime factors of i. Finally sum all phi(i).

def count_irr_2(m,n) :
    primes = eratostene(n)
    nr = 0
    for i in range (m, n+1) :
        k = 2
        phi_i = i
        check = i
        while k <= i :
           if primes[k-2] and i%k == 0 :
                phi_i = phi_i*(1-1/k)
                check = int(i/k)
           elif primes[k-2] and k > check:
               break
           k += 1
        nr += phi_i
    return int(nr)



def eratostene(n):
    num = [True]*(n-1)
    i = 2
    while(i*i <= n):
        if num[i-2] == True :
            j = i*i-2
            while( j < n-1 ):
                num[j] = False
                j = j + i
        i += 1
    return num

- Luke Rhinehart August 17, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More