1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cmath>
#include <set>
using namespace std;

set< set<int> > primitives;

int nwd(int a, int b) {
  if(a == 0)
    return b;
  if(b == 0)
    return a;
  return nwd(b,a%b);
}

int nwd(int a, int b, int c, int d) {
  return nwd(nwd(a,b), nwd(c,d));
}

const int PIERW5000 = round(sqrt(5000))+1;

int main() 
{
  int nn;
  cin >> nn;
  
  int licznik = 0;
  
  for(int n = 0; n < PIERW5000; n++)
    for(int m = 0; m < PIERW5000; m++)
	  for(int q = 0; q < PIERW5000; q++)
	    for(int p = 0; p < PIERW5000; p++) {
		  if( (m+n+p+q) % 2 == 0 || nwd(m,n,p,q) != 1)
		    continue;
		  
		  int a = m*m + n*n - p * p - q*q;
		  int b = 2*(m*q + n * p);
		  int c = 2*(n*q-m*p);
		  int d = m*m + n*n + p*p + q*q;
		  
		  
		  
		  if(a <= 0 || b <= 0 || c <= 0)
			  continue;
		  
		  if(nwd(a,b,c,d) != 1)
			  continue;
		  
		  	  
		  if(d > nn)
		    break;
			
		  set<int> s = {a,b,c};
		  if( primitives.find(s) == primitives.end() ) {
		    primitives.insert(s);
			
			
			int krotnosc = nn / d;
			if(a == b && b == c) {
			  licznik += krotnosc;
			} else {
			  if(a == b || b == c || a == c) {
			    licznik += krotnosc * 2;
			  }
			  else {
			    licznik += krotnosc * 3;
			  }
			}
		}		  
		}
  
  cout << licznik << endl;
  
  return 0;
}