// 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;
  }
Ejemplo n.º 2
0
  public static void exporter(LinkedList<profile> usr, MatlabProxy proxy)
      throws MatlabConnectionException, MatlabInvocationException {

    String file = "/Users/richarddavies/NetBeansProjects/typ_MatlabGraph/rotCmp_2012.txt";
    int i = 0;

    try {

      double result;
      int index_a = 0;
      int index_b = 0;
      int item;

      FileWriter fw = new FileWriter(file);
      BufferedWriter bw = new BufferedWriter(fw);
      PrintWriter outFile = new PrintWriter(bw);

      outFile.println("nodedef>name VARCHAR,label VARCHAR,sex VARCHAR,locale VARCHAR");

      for (i = 0; i < usr.size(); i++) {

        outFile.println(
            usr.get(i).idProf
                + ","
                + usr.get(i).firstName
                + ","
                + usr.get(i).gender
                + ","
                + usr.get(i).nat);
      }

      outFile.println("edgedef>node1 VARCHAR,node2 VARCHAR");

      for (i = 0; i < usr.size(); i++) {

        System.out.println(usr.get(i).firstName + ": ");

        for (int j = 0; j < usr.size(); j++) {

          index_a = i + 1;
          index_b = j + 1;

          // result = proxy.getVariable("adjMatrix("+index_a+","+index_b+");");

          result = ((double[]) proxy.getVariable("adjMatrix(" + index_a + "," + index_b + ");"))[0];

          item = (int) result;

          // item = ((Integer)result).intValue();
          // System.out.println(result);
          if (item == 1) {

            System.out.print(" (" + usr.get(j).firstName + ") ");

            outFile.println(usr.get(i).idProf + "," + usr.get(j).idProf);
          }
        }

        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");
      }

      outFile.close();
    } catch (IOException ex) {
      System.out.println(" error : " + ex);
    }
  }