All square roots are periodic when written as continued fractions.

#codeifyoucansolve
All square roots are periodic when written as continued fractions.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        final int MAX=30000;
        int result=0;
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        if(N>0&&N<=MAX)
        {       
            for(int i=2;i<=N;i++)
            {
           int lmt=(int)Math.sqrt(i);
                if(lmt*lmt==i)
                    continue;
           int period = 0;
           int d = 1;
           int m = 0;
           int a = lmt;      
                do{                
                    m = d*a - m;                    
                    d = (i - m * m) / d;
                    a = (lmt + m) / d;
                    period++;
                }while(a != 2*lmt);
            if((period%2)==1)
                result++;
            }
            System.out.print(result);
        }
    }
}