Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:21:37+00:00 2026-05-27T18:21:37+00:00

I came across this problem of finding said probability and my first attempt was

  • 0

I came across this problem of finding said probability and my first attempt was to come up with following algorithm: I am counting number of pairs which are relatively prime.

int rel = 0
int total = n * (n - 1) / 2
for i in [1, n)
    for j in [i+1, n)
        if gcd(i, j) == 1
            ++rel;
return rel / total

which is O(n^2).

Here is my attempt to reducing complexity:

Observation (1): 1 is relatively prime to [2, n] so n - 1 pairs are trivial.

Observation (2): 2 is not relatively prime to even numbers in the range [4, n] so remaining odd numbers are relatively prime to 2, so

 #Relatively prime pairs = (n / 2) if n is even 

                         = (n / 2 - 1) if n is odd.

So my improved algorithm would be:

int total = n * (n - 1) / 2
int rel = 0
if (n % 2) // n is odd
    rel = (n - 1) + n / 2 - 1
else // n is even
    rel = (n - 1) + n / 2

for i in [3, n)
    for j in [i+1, n)
        if gcd(i, j) == 1
            ++rel;
return rel / total

With this approach I could reduce two loops, but worst case time complexity is still O(n^2).

Question: My question is can we exploit any mathematical properties other than above to find the desired probability in linear time?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:21:38+00:00Added an answer on May 27, 2026 at 6:21 pm

    You’ll need to calculate the Euler’s Totient Function for all integers from 1 to n. Euler’s totient or phi function, φ(n), is a arithmetical function that counts the number of positive integers less than or equal to n that are relatively prime to n.

    To calculate the function efficiently, you can use a modified version of Sieve of Eratosthenes.

    Here is a sample C++ code –

    #include <stdio.h>
    
    #define MAXN 10000000
    
    int phi[MAXN+1];
    bool isPrime[MAXN+1];
    
    void calculate_phi() {
        int i,j;
        for(i = 1; i <= MAXN; i++) {
            phi[i] = i;
            isPrime[i] = true;
        }
    
        for(i = 2; i <= MAXN; i++) if(isPrime[i]) {
            for(j = i+i; j <= MAXN; j+=i) {
                isPrime[j] = false;
                phi[j] = (phi[j] / i) * (i-1);
            }
        }
    
        for(i = 1; i <= MAXN; i++) {
            if(phi[i] == i) phi[i]--;
        }
    }
    
    int main() {
        calculate_phi();
        return 0;
    }
    

    It uses the Euler’s Product Formula described on the Wikipedia page of Totient Function.

    Calculating the complexity of this algorithm is a bit tricky, but it is much less than O(n^2). You can get results for n = 10^7 pretty quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Basic Recursion, Check Balanced Parenthesis I came across this problem in Algorithm
I came across this problem, I have a number of type long which represents
I came across this problem,I have got an algorithm that I need to implement
I came across this problem from CodeChef . The problem states the following: A
I came across this problem when I want to check what I input is
I came across this problem when trying to decode json into an array ,
i came across this problem, my code in the viewWillAppear method is not executed
I'm using GAE with python and I came across this problem, I'm trying to
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
I was configuring syntax highlighting for my blog when I came across this problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.