tenebrix's blog

By tenebrix, 7 hours ago, In English

Now finding a coprime to a given number n(<= $$$10^{18}$$$) we might think it may take O(n) to iterate a loop of i=2 to i=n-1 and say if test cases t <= $$$10^5$$$ and no further restrictions on n and t — we might think of it as high complexity method but in actual — does it take O(n) ?

Let's see

the worst case scenario we fear — loop is running and we haven't found a number coprime to it -> which means it has all prime factors(2,3,5,7....23,29,31,37...) till that

but

a number having all primes till 29 is at least = 2*3*5*.....23*29 which exceeds $$$10^9$$$

a number having all primes till 53 is at least = 2*3*5*.....23*29...43*47*53 which exceeds $$$10^{18}$$$

thus we can say

  • if a loop runs till 47 and no coprime has been found :
    • we have 2 things guaranteed :
    • the number is near its limit $$$10^{18}$$$
    • the next prime 53 is coprime to n as if it isn't then it will exceed limit which isn't possible
  • else a loop doesn't run successfully till 47 and we have found a number coprime to n earlier than 47 iterations

Thus it takes less than 60 iterations which is O(1)

and its safe to run a loop of i=2 to i=n-1 or even better run

const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};

to find a coprime number to given number n

Reference question : 1617B - GCD Problem (though this question also has a better approach than this one)

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
7 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by tenebrix (previous revision, new revision, compare).

»
4 hours ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

learned what coprime numbers are, cool

but isn't a coprime js two numbers whose numbers (larger) % (smaller) = 1, so like 16 and 1 would be coprimes despite 16 not actually being a prime number

so if you are given a number and you have to find another number that is coprime you could always use 1 since every number mod 1 evaluated to 1? (I might be wrong, so sorry if that's not what ur asking)

  • »
    »
    101 minute(s) ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    Partially correct. Two numbers $$$a$$$ and $$$b$$$ are coprime if $$$\gcd(a, b)=1$$$. It is true that by definition $$$1$$$ is coprime to $$$16$$$, but this solution is used in the context where one is unable to use the number $$$1$$$.

    Also, $$$16\bmod 1$$$ evaluates to $$$0$$$ because $$$1\mid 16$$$. $$$a\bmod b$$$ evaluates to $$$1$$$ if and only if the remainder of $$$a/b$$$ is $$$1$$$. It seems you are assuming $$$a\bmod b$$$ will evaluate to $$$1$$$ if $$$b\nmid a$$$, but this is untrue. For example, $$$5\bmod 3 = 2$$$ and $$$2\nmid 5$$$.

    If I remember correctly, 2167D - Yet Another Array Problem uses this technique as well!

    Apologies for any minor logical or syntactical issues

    • »
      »
      »
      63 minutes ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      yea I kinda realized that anything mod 1 js equals zero. Also in the case of not being able to use one couldn't you use sieve of eratherasnos or whatever his name is (idk if that's smth you could acc do, but ik it's related to finding primes from 1 to n)

      • »
        »
        »
        »
        35 minutes ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        You could, but as this post states, you only need to find primes up to and including $$$53$$$. This is a very inexpensive calculation so you could do it, or you could list out all primes by hand (as the post suggests). Using the Sieve of Eratosthenes just seems unneeded in this case but it will accomplish the goal.

        • »
          »
          »
          »
          »
          16 minutes ago, hide # ^ |
           
          Vote: I like it +1 Vote: I do not like it

          ty for helping me find my silly mistakes tho, so like 67 and 420 (hehe funny numbers) are coprime even though 420 % 67 isn't 1,cus they don't share a common divisor except 1. still getting used to this number theory math stuff

»
4 minutes ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Or you can choose n — 1.