Finding Coprime triplets Given a sequence a1, a2, ..., aN. Count the number of triples (i, j, k) such that 1 ≤ i < j < k ≤ N and GCD(ai, aj, ak) = 1. Here GCD stands for the Greatest Common Divisor.
Example : Let N=4 and array be : 
1 2 3 4
Here answer is 4.
I want to calculate it if N is upto 10^5.
 A: As noted in the comments, this doesn't answer the question at hand, but one that is perhaps closely enough related to give some ideas on how to proceed. 
At https://projecteuler.chat/viewtopic.php?f=17&t=2889 Guego has written, 
"It's quite easy to count the number $C(n)$ of triples $(a,b,c)$ with $1\leq a,b,c \leq n$ which are coprime ($\gcd(a,b,c)=1$).  
"Indeed, using the formula $n^3 = \sum_d C(n/d)$, we can have $C(n)$ quite easily using a recursive algorithm, or with the Möbius inversion formula. 
"I implemented it in python, and my program gives, for example, $$C(1000000) = 831907430687799769$$ in less than one second."
Guego later adds, "the sum is over all the possible values of $d$ (not just the divisors of $n$), but $n/d$ must be understood as an integer division."
A: If we assume absolutely nothing about the $\{a_i\}_{i=1}^N$...
Make a graph.  The set of vertices of the graph, $V$, are the $a_i$.  Add an edge between the vertices $a_i$ and $a_j$ labelled $\gcd(a_i,a_j)$.  It is convenient to keep the collection of edges, $E$, and also, for each distinct $\gcd$ the set of labels having that $\gcd$, call it $E_g$ for $g \in \{\gcd(a_i,a_j) \mid a_i,a_j \in V\}$.
For each edge $e \in E$, labelled $t$, construct the graph with edges $e$ and the union of all the $E_g$ with $\gcd(g,t)=1$.  (It may be helpful to construct the graph of pairs of $E_g$ that have relatively prime labels and cache these unions, since you may need them many times.)  Count the number of triangles in this constructed graph.  (Note that the adjacency matrices used at the given link are the unions of the adjacency matrices for the various $E_g$.)  Accumulate these counts for each $e$ to get the total count of coprime triples.
This algorithm can easily take $O(N^3)$ time and $O(N^2)$ space.  The trivial solution (not described here) also takes $O(N^3)$ time but only $O(1)$ space.  In spite of this, I expect the above to be much faster than the trivial solution given the amount of redundant work it avoids.
Aside:  My first try at a solution counted triples of edges with label "1", but this is incorrect since $\{6,10,15\}$ would not be found by this method, assuming those integers are in the $a_i$.
