Guest User

Untitled

a guest
Apr 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package problem69;
  2.  
  3. public class Problem69 {
  4.  
  5.     static boolean[] sieve;
  6.  
  7.     public static void calculateSieve(int x) {
  8.         sieve = new boolean[x];
  9.         for (int i = 2; i < sieve.length; i++) {
  10.             sieve[i] = true;
  11.         }
  12.  
  13.         for (int i = 0; i < sieve.length; i++) {
  14.             if (sieve[i] == true) {
  15.                 for (int j = i * 2; j < sieve.length; j += i) {
  16.                     sieve[j] = false;
  17.                 }
  18.             }
  19.         }
  20.     }
  21.  
  22.     public static int totientFunciton(int nmbr) {
  23.         double lab = nmbr;
  24.         for (int i = 1; i < nogrannhet; i++) {
  25.             if (nmbr % i == 0 && sieve[i]) {
  26.                 //      System.out.println(i);
  27.                 lab = lab * (1 - (1 / (double) i));
  28.             }
  29.         }
  30.         //System.out.println(lab);
  31.         return (int) lab;
  32.     }
  33.     static int nogrannhet = 18;
  34.  
  35.     public static void main(String[] args) {
  36.         long beg = System.currentTimeMillis();
  37.         calculateSieve(nogrannhet);
  38.  
  39.         double rekord = 0;
  40.  
  41.         for (int i = 10; i < 1000000; i++) {
  42.             if ((double) i / totientFunciton(i) > rekord) {
  43.                 rekord = (double) i / totientFunciton(i);
  44.                 System.out.println("Nytt rekord för i = " + i + " - " + rekord);
  45.             }
  46.         }
  47.  
  48.  
  49.         long end = System.currentTimeMillis();
  50.         System.out.println("Det tog " + (end - beg) + " ms att beräkna.");
  51.     }
  52. }
Add Comment
Please, Sign In to add comment