@matfah/

Self Divisor FR 2007

Java

No description

fork
loading
Files
  • Main.java
  • SelfDivisor.java

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7853094059827579","path":"Main.java","file":{"path":"Main.java","content":{"asEncoding":{"base64":"aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogIAogIHByaXZhdGUgc3RhdGljIFRyZWVTZXQ8SW50ZWdlcj4gc2V0OwogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIHNldCA9IG5ldyBUcmVlU2V0PEludGVnZXI+KCk7CiAgICBmb3IoaW50IHggOiBzZCkKICAgICAgc2V0LmFkZCh4KTsKICAgICAgCiAgICB0ZXN0SXNTZWxmRGl2aXNvcigpOwogICAgU3lzdGVtLm91dC5wcmludGxuKCJQYXNzZWQgaXNTZWxmRGl2aXNvcigpIHRlc3RzISIpOwogICAgCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgMjA7IGkrKykKICAgICAgdGVzdEZpcnN0TnVtU2VsZkRpdmlzb3JzKCk7CiAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlBhc3NlZCBmaXJzdE51bVNlbGZEaXZpc29ycygpIHRlc3RzISIpOwogICAgCiAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlBhc3NlZCBBbGwgVGVzdHMhIik7CiAgfQogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCB0ZXN0SXNTZWxmRGl2aXNvcigpIHsKICAgIAogICAgaW50W10gY2FzZXMgPSB7MCwgNSwgMTAsIDEwNCwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKSwgKGludCkoTWF0aC5yYW5kb20oKSoxMDAwKX07CiAgICAKICAgIGZvcihpbnQgbjogY2FzZXMpIHsKICAgICAgdHJ5IHsKICAgICAgICAKICAgICAgICBib29sZWFuIHJlc3VsdCA9IFNlbGZEaXZpc29yLmlzU2VsZkRpdmlzb3Iobik7CiAgICAgICAgaWYocmVzdWx0ICE9IHNldC5jb250YWlucyhuKSkgewogICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJSZXN1bHQgb2YgY2FsbGluZyBpc1NlbGZEaXZpc29yKCIgKyBuICsgIikgaXMgd3JvbmciKTsKICAgICAgICAgIFN5c3RlbS5leGl0KDApOwogICAgICAgIH0KICAgICAgICAKICAgICAgfQogICAgICBjYXRjaChFeGNlcHRpb24gZSkgewogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiRXJyb3IgY2FsbGluZyBpc1NlbGZEaXZpc29yKCIgKyBuICsgIikiKTsKICAgICAgICBTeXN0ZW0uZXhpdCgwKTsKICAgICAgfQogICAgfQogICAgCiAgfQogIAogIHB1YmxpYyBzdGF0aWMgdm9pZCB0ZXN0Rmlyc3ROdW1TZWxmRGl2aXNvcnMoKSB7CiAgICBpbnQgc3RhcnQgPSAoaW50KShNYXRoLnJhbmRvbSgpICogMTAwKTsKICAgIGludCBudW0gPSAoaW50KShNYXRoLnJhbmRvbSgpKjIwKSArIDE7CiAgICAKICAgIGludFtdIGdvYWwgPSBuZXcgaW50W251bV07CiAgICAKICAgIGludCBwb3MgPSAwOwogICAgd2hpbGUoc2RbcG9zXSA8IHN0YXJ0KQogICAgICBwb3MrKzsKICAgICAgCiAgICBpbnQgaSA9IDA7CiAgICB3aGlsZShpIDwgbnVtKSB7CiAgICAgIGdvYWxbaV0gPSBzZFtwb3NdOwogICAgICBpKys7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4OCw5OSwxMTEsMTEyLDExNSwxMjIsMTI0LDEyNiwxMjgsMTMyLDEzNSwxNDQsMTU1LDE2MiwxNjgsMTc1LDE4NCwyMTIsMjE2LDIyMiwyMjQsMjQ0LDI0OCwyNjQsMjg4LDMxMiwzMTUsMzI0LDMzMywzMzYsMzY2LDM4NCwzOTYsNDEyLDQyNCw0MzIsNDQ0LDQ0OCw0ODgsNTE1LDU1NSw2MTIsNjI0LDYzNiw2NDgsNjY2LDY3Miw3MjgsNzM1LDc3Nyw3ODQsODE2LDgyNCw4NDgsODY0LDg4OCw5MzYsOTk5fTsKfQ=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.*;

public class Main {
  
  private static TreeSet<Integer> set;
  
  public static void main(String[] args) {
    set = new TreeSet<Integer>();
    for(int x : sd)
      set.add(x);
      
    testIsSelfDivisor();
    System.out.println("Passed isSelfDivisor() tests!");
    
    for(int i = 0; i < 20; i++)
      testFirstNumSelfDivisors();
    System.out.println("Passed firstNumSelfDivisors() tests!");
    
    System.out.println("Passed All Tests!");
  }
  
  public static void testIsSelfDivisor() {
    
    int[] cases = {0, 5, 10, 104, (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000), (int)(Math.random()*1000)};
    
    for(int n: cases) {
      try {
        
        boolean result = SelfDivisor.isSelfDivisor(n);
        if(result != set.contains(n)) {
          System.out.println("Result of calling isSelfDivisor(" + n + ") is wrong");
          System.exit(0);
        }
        
      }
      catch(Exception e) {
        System.out.println("Error calling isSelfDivisor(" + n + ")");
        System.exit(0);
      }
    }
    
  }
  
  public static void testFirstNumSelfDivisors() {
    int start = (int)(Math.random() * 100);
    int num = (int)(Math.random()*20) + 1;
    
    int[] goal = new int[num];
    
    int pos = 0;
    while(sd[pos] < start)
      pos++;
      
    int i = 0;
    while(i < num) {
      goal[i] = sd[pos];
      i++;
      pos++;
    }
    
    try {
      int[] result = SelfDivisor.firstNumSelfDivisors(start, num);
      
      if(goal.length != result.length) {
        System.out.println("Array returned by isSelfDivisor(" + start + ", " + num + ") is wrong length!");
        System.exit(0);
      }
      
      for(int k = 0; k < result.length; k++) {
        if(goal[k] != result[k]) {
          System.out.println("Array returned by isSelfDivisor(" + start + ", " + num + ") contains the wrong values!");
          System.exit(0);
        }
      }
      
    }
    catch(Exception e) {
      System.out.println("Error calling isSelfDivisor(" + start + ", " + num + ")");
      System.exit(0);
    }
  }
  
  //just test the self divisors less than 1000...
  private static int[] sd = {0,1,2,3,4,5,6,7,8,9,11,12,15,22,24,33,36,44,48,55,66,77,88,99,111,112,115,122,124,126,128,132,135,144,155,162,168,175,184,212,216,222,224,244,248,264,288,312,315,324,333,336,366,384,396,412,424,432,444,448,488,515,555,612,624,636,648,666,672,728,735,777,784,816,824,848,864,888,936,999};
}