コード例 #1
0
ファイル: Rules.java プロジェクト: bitsai/courses
  /** Returns distance between specified coordinates. */
  public static double getDistance(Point origin, Point destination) {
    int x = destination.getx() - origin.getx();
    int y = destination.gety() - origin.gety();
    double distance = Math.sqrt(x * x + y * y);

    return distance;
  }
 public static boolean isPrime(long n) throws Exception {
   if (n == 1) return false;
   if (n == 2 || n == 3) return true;
   for (int i = 2; i <= Math.sqrt(n); i++) {
     if (n % i == 0) return false;
   }
   return true;
 }
コード例 #3
0
 static double[] PDF() {
   double normal[] = new double[256];
   for (int x = 0; x < 256; x++) {
     float sigma = 50;
     float m = 128;
     double sigmasqr2pi = sigma * Math.sqrt(2 * Math.PI);
     float twosigma2 = 2 * sigma * sigma;
     normal[x] = Math.exp(-(Math.pow((x - m), 2)) / twosigma2) / (sigmasqr2pi);
   }
   return normal;
 }
コード例 #4
0
ファイル: 1245.java プロジェクト: pallab-gain/LightOJ
  public void solve1245(int kaseno) throws Exception {
    int N = iread();
    int sqrtN = (int) Math.sqrt((double) N);
    long sum1 = 0;
    for (int i = 1; i <= sqrtN; ++i) {
      sum1 += (int) (N / i);
    }
    sum1 <<= 1;

    long sum2 = sqrtN * sqrtN;
    long ret = sum1 - sum2;
    out.write("Case " + kaseno + ": " + ret + "\n");
  }
コード例 #5
0
	//computes all primes up to some n and store them the queue 
	public void computeTo(int n)
	{
		// add int 2:n into queue
		int i=2;
		int count=0;
		int value1=0;
		int value2=0;
		int size=0;
		int whilesize=(int)Math.sqrt(n);
		
		while(i<=n)// n=10
		{
			queueNumbers.enqueue(i);//2 3 4 5 6 7 8 9 10
			i++;
		}
		do{
		value1= (int)queueNumbers.dequeue(); //value1=2, 
		queuePrime.enqueue(value1);// 2  
		for (int count=0; count<queueNumbers.getsize(); count++)// 0-10
		{
			value2=(int)queueNumber.dequeue();// 3 
			if(value2%value1 !=0)//3%2 !=0  
			{
				queueNumbers.enqueue(value2);// store 3 in queueNumbers,
			}
		}
		}// end do
		while(value2 <= whilesize) 
		{
			size= queueNumbers.getsize();
			for(int k=0; i<size; i++)
			{
				queueNumbers.dequeue(value2);
				queuePrime.enqueue(value2);
			}
		}// end while 
		
/**		
create a queue and fill it with the consecutive integers 2 through n inclusive.
create an empty queue to store primes.
do {
  obtain the next prime by removing the first value in the queue of numbers.
  put the next prime into the queue of primes.
  go through the queue of numbers, eliminating numbers divisible by the next prime.
} while (the next prime < sqrt(n))
all remaining values in numbers queue are prime, so transfer them to primes queue
*/
	}
コード例 #6
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    l:
    while (true) {
      int n = input.nextInt();
      if (n == 0) {
        break;
      }
      if (n < 3) {
        System.out.println("No such base");
        continue l;
      }
      if (n == 3) {
        System.out.println("4");
        continue l;
      }
      int tmp = n - 3;
      for (int base = 4; base <= Math.sqrt(n) + 1; base++) {
        if (tmp % base == 0) {
          System.out.println(base);
          continue l;
        }
      }
      if (tmp % 3 == 0) {
        if (tmp / 3 > 3) {
          System.out.println(tmp / 3);
          continue l;
        }
      }
      if (tmp % 2 == 0) {
        if (tmp / 2 > 3) {
          System.out.println(tmp / 2);
          continue l;
        }
      }
      if (tmp > 3) {
        System.out.println(tmp);
        continue l;
      }

      System.out.println("No such base");
      // for(int base = 1; base < )
    }
  }
コード例 #7
0
 public double dist(int mx, int my) {
   try {
     double ya = ma.y;
     double yb = mb.y;
     double xa = ma.x;
     double xb = mb.x;
     double S = (yb - ya) / (xb - xa);
     double ptx = (my - ya + mx / S + S * xa) / (S + 1 / S);
     double pty = ya + S * (ptx - xa);
     double maxx = xa;
     double minx = xb;
     double maxy = ya;
     double miny = yb;
     if (maxx < minx) {
       maxx = xb;
       minx = xa;
     }
     if (maxy < miny) {
       maxy = yb;
       miny = ya;
     }
     if (ptx > maxx) {
       ptx = maxx;
     }
     if (ptx < minx) {
       ptx = minx;
     }
     if (pty > maxy) {
       pty = maxy;
     }
     if (pty < miny) {
       pty = miny;
     }
     System.out.println(ptx + " " + pty + " ");
     return Math.sqrt((mx - ptx) * (mx - ptx) + (my - pty) * (my - pty));
   } catch (Exception e) {
     System.out.println(e.getMessage());
   }
   return 0;
 }
コード例 #8
0
  // sieve
  public static int[] primes(int n)
      throws Exception { // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" ");
    boolean arr[] = new boolean[n + 1];
    Arrays.fill(arr, true);
    for (int i = 1; i <= Math.sqrt(n); i++) {
      if (!arr[i]) continue;
      for (int j = 2 * i; j <= n; j += i) {
        arr[i] = false;
      }
    }
    LinkedList<Integer> ll = new LinkedList<Integer>();
    for (int i = 1; i <= n; i++) {
      if (arr[i]) ll.add(i);
    }
    n = ll.size();

    int primes[] = new int[n + 1];
    for (int i = 1; i <= n; i++) {
      primes[i] = ll.removeFirst();
    }
    return primes;
  }
コード例 #9
0
ファイル: Solution.java プロジェクト: mlin6436/dojo4j
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String s = in.next();

    int l = s.length();
    Double sqrl = Math.sqrt(l);
    Double low = Math.floor(sqrl);
    Double high = Math.ceil(sqrl);
    int r = 0;
    int c = 0;

    if (low == high) {
      r = low.intValue();
      c = low.intValue();
    } else if (low * high >= l) {
      r = low.intValue();
      c = high.intValue();
    } else {
      r = high.intValue();
      c = high.intValue();
    }

    //        System.out.println("Row: " + r);
    //        System.out.println("Col: " + c);

    StringBuilder sb = new StringBuilder();
    char en[] = s.toCharArray();
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        if (j * c + i < l) {
          sb.append(en[j * c + i]);
        }
      }
      sb.append(" ");
    }

    System.out.println(sb.toString());
  }
コード例 #10
0
 public double massdist() {
   return Math.sqrt((ma.x - mb.x) * (ma.x - mb.x) + (ma.y - mb.y) * (ma.y - mb.y));
 }
コード例 #11
0
ファイル: H.java プロジェクト: Mityai/nncworkspaces
 double len() {
   return Math.sqrt(x * x + y * y);
 }
コード例 #12
0
ファイル: H.java プロジェクト: Mityai/nncworkspaces
 double dist(Point p) {
   return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
 }
コード例 #13
0
ファイル: AOJ0081.java プロジェクト: t8m8/AOJ
 public double length() {
   return Math.sqrt(this.norm());
 }
コード例 #14
0
 double helper(long a, long b) {
   return 2 * Math.sqrt(a * b);
 }
コード例 #15
0
 // To add/remove functions change evaluateOperator() and registration
 public double evaluateFunction(String fncnam, ArgParser fncargs) throws ArithmeticException {
   switch (Character.toLowerCase(fncnam.charAt(0))) {
     case 'a':
       {
         if (fncnam.equalsIgnoreCase("abs")) {
           return Math.abs(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("acos")) {
           return Math.acos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("asin")) {
           return Math.asin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("atan")) {
           return Math.atan(fncargs.next());
         }
       }
       break;
     case 'c':
       {
         if (fncnam.equalsIgnoreCase("cbrt")) {
           return Math.cbrt(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("ceil")) {
           return Math.ceil(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cos")) {
           return Math.cos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cosh")) {
           return Math.cosh(fncargs.next());
         }
       }
       break;
     case 'e':
       {
         if (fncnam.equalsIgnoreCase("exp")) {
           return Math.exp(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("expm1")) {
           return Math.expm1(fncargs.next());
         }
       }
       break;
     case 'f':
       {
         if (fncnam.equalsIgnoreCase("floor")) {
           return Math.floor(fncargs.next());
         }
       }
       break;
     case 'g':
       {
         //              if(fncnam.equalsIgnoreCase("getExponent"   )) { return
         // Math.getExponent(fncargs.next());                } needs Java 6
       }
       break;
     case 'l':
       {
         if (fncnam.equalsIgnoreCase("log")) {
           return Math.log(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log10")) {
           return Math.log10(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log1p")) {
           return Math.log1p(fncargs.next());
         }
       }
       break;
     case 'm':
       {
         if (fncnam.equalsIgnoreCase("max")) {
           return Math.max(fncargs.next(), fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("min")) {
           return Math.min(fncargs.next(), fncargs.next());
         }
       }
       break;
     case 'n':
       {
         //              if(fncnam.equalsIgnoreCase("nextUp"        )) { return Math.nextUp
         // (fncargs.next());                } needs Java 6
       }
       break;
     case 'r':
       {
         if (fncnam.equalsIgnoreCase("random")) {
           return Math.random();
         } // impure
         if (fncnam.equalsIgnoreCase("round")) {
           return Math.round(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("roundHE")) {
           return Math.rint(fncargs.next());
         } // round half-even
       }
       break;
     case 's':
       {
         if (fncnam.equalsIgnoreCase("signum")) {
           return Math.signum(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sin")) {
           return Math.sin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sinh")) {
           return Math.sinh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sqrt")) {
           return Math.sqrt(fncargs.next());
         }
       }
       break;
     case 't':
       {
         if (fncnam.equalsIgnoreCase("tan")) {
           return Math.tan(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("tanh")) {
           return Math.tanh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toDegrees")) {
           return Math.toDegrees(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toRadians")) {
           return Math.toRadians(fncargs.next());
         }
       }
       break;
     case 'u':
       {
         if (fncnam.equalsIgnoreCase("ulp")) {
           return Math.ulp(fncargs.next());
         }
       }
       break;
       // no default
   }
   throw new UnsupportedOperationException(
       "MathEval internal function setup is incorrect - internal function \""
           + fncnam
           + "\" not handled");
 }
コード例 #16
0
ファイル: Solution.java プロジェクト: mlin6436/dojo4j
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int row = in.nextInt();
    int col = in.nextInt();
    BigInteger r = in.nextBigInteger();

    //        System.out.println(row);
    //        System.out.println(col);
    //        System.out.println(r);

    int m[][] = new int[row][col];
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++) {
        m[i][j] = in.nextInt();
      }
    }

    //        for (int i = 0; i < row; i++) {
    //            for (int j = 0; j < col; j++) {
    //                System.out.print(m[i][j]);
    //            }
    //            System.out.println();
    //        }

    //        System.out.println(Math.ceil(Math.sqrt(col)));
    for (int n = 0; n < Math.ceil(Math.sqrt(col)); n++) {
      int max_row = row - n;
      int max_col = col - n;
      //            System.out.println("New array size: " + size);
      List<Integer> a = new ArrayList<Integer>();

      for (int i = n; i < max_row - 1; i++) {
        a.add(m[i][n]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      for (int i = n; i < max_col - 1; i++) {
        a.add(m[max_row - 1][i]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      for (int i = max_row - 1; i > n; i--) {
        a.add(m[i][max_col - 1]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      for (int i = max_col - 1; i > n; i--) {
        a.add(m[n][i]);
      }
      //            for (int i : a) {
      //                System.out.print(i);
      //            }
      //            System.out.println();

      if (n == max_row - 1) {
        for (int i = n; i < max_col - n; i++) {
          a.add(m[n][i]);
        }
      }

      for (int i : a) {
        System.out.print(i);
      }
      System.out.println();
    }
  }