Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner s = new Scanner(System.in);
  8.         int size = s.nextInt();
  9.         for (int i = 0; i < size; i++) {
  10.             System.out.print(lampa(s.nextInt()) + " ");
  11.         }
  12.     }
  13.  
  14.     public static int lampa(int a) {
  15.         int razm = 2000000;
  16.         int prost[]=new int[10000000];
  17.         boolean mas[] = new boolean[10000000];
  18.         for(int i=1;i<=Math.sqrt(razm);i++) {
  19.  
  20.             for(int j=1;j<=Math.sqrt(razm);j++) {
  21.  
  22.                 int k = 0, f1 = 4 * i * i + j * j, f2 = 3 * i * i + j * j, f3 = 3 * i * i - j * j;
  23.                 if (((f1) % 12 == 1) || ((f1) % 12 == 5)) {
  24.                     k++;
  25.                     mas[f1] = true;
  26.                 }
  27.                 if ((f2) % 12 == 7) {
  28.                     k++;
  29.                     mas[f2] = true;
  30.                 }
  31.                 if ((i > j) && (f3) % 12 == 11) {
  32.                     k++;
  33.                     mas[f3] = true;
  34.                 }
  35.                 if (k > 1) {
  36.                     if (f1 == f2) mas[f1] = false;
  37.                     else if (f1 == f3) mas[f1] = false;
  38.                     else if (f2 == f3) mas[f2] = false;
  39.  
  40.                 }
  41.  
  42.             }
  43.  
  44.         }
  45.         for (int i=1;i<=razm;i++)
  46.         for (int j=5;j<=Math.sqrt(razm);j++)
  47.             if(mas[i]==true&&mas[j]==true&&(i%(j*j)==0))
  48.                 mas[i]=false;
  49.         int j=3;
  50. for (int i=0;i<razm+3100;i++)
  51.     if(mas[i]==true) {
  52.         prost[j] = i;
  53.         j++;
  54.     }
  55. prost[1]=2;
  56.         prost[2]=3;
  57.         //a) 4*x^2+y^2;
  58.         //b) 3*x^2+y^2;
  59.         //c) 3*x^2-y^2, значение вычисляется только при x>y.
  60. return prost[a];
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement