Problem:
Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692

Solution in Fortran (GNU Fortran 9.2.1) (judge verdict: Accepted)

program main
  implicit none

  integer :: n, i, j, k, s

  read *, n

  s = 0
  do i = 1, n
     do j = i, n
        do k = j, n
           if (i == j) then
              if (j == k) then
                 s = s + gcd(i,j,k)
              else
                 s = s + 3*gcd(i,j,k)
              end if
           else
              if (j == k) then
                 s = s + 3*gcd(i,j,k)
              else
                 s = s + 6*gcd(i,j,k)
              end if
           end if
        end do
     end do
  end do

  print *, s
  

  stop
contains

  integer function gcd(i,j,k)
    implicit none
    integer, intent(in) :: i, j, k
    integer :: a, b, c, d, e
    logical :: log

    a = i
    b = j
    c = k
    if (a < b) then
       d = a
       a = b
       b = d
    end if

    log = .true.
    do while(log .eqv. .true.)
       if (b == 0) then
          d = a
          log = .false.
          exit
       else
          d = mod(a,b)
          a = b
          b = d
       end if
    end do

    if (c < d) then
       e = c
       c = d
       d = e
    end if

    log = .true.
    do while(log .eqv. .true.)
       if (d == 0) then
          gcd = c
          log = .false.
          exit
       else
          e = mod(c,d)
          c = d
          d = e
       end if
    end do

    return
  end function gcd
    
end program main