All Tracks Problem

KGCD
Tag(s):

Math, Medium

Problem
Editorial
Analytics

Kevin recently learned a new algorithm called Greatest Common Divisor. He tried to implement it but he failed. He called this new algorithm Kevin's Greatest Common Divisor (KGCD). You can look at this implementation:

long long kgcd(long long a, long long b)
{
    while (a > 0 && b > 0)
    {
        a -= b;
        swap(a , b);
    }
    return a + b;
}

Now Kevin runs $$kgcd(a,b)$$ for all $$1 \le a \le N$$, $$1 \le b \le N$$. He is interested in how many times his algorithm returns the correct gcd value of $$a$$ and $$b$$.

Input format:

The first line of input will contain an integer $$T$$, denoting the number of test cases.
Each of the next $$T$$ lines contain one integer $$N$$.

Output format:

For every test case output the number of times $$kgcd(a,b)==gcd(a,b)$$.

Constraints:

  • $$1 \le T \le 10$$
  • (20 points): $$1 \le N \le 1000$$
  • (30 points): $$1 \le N \le 10^6$$
  • (50 points): $$1 \le N \le 10^{12}$$
SAMPLE INPUT
2
4
2
SAMPLE OUTPUT
14
4
Explanation

In the first case $$kgcd$$ works correct for all $$a,b$$ except $$(2,3)$$ and $$(3,4)$$.

Time Limit: 10.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Scala 2.11.8, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

September Clash '16

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications