1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public
16class Piep {
17
18
19
20
21
22
23 public final
24 int ziffer;
25
26
27
28
29 public final
30 int basis;
31
32
33
34
35
36
37
38
39
40 public
41 Piep(int ziffer, int basis) {
42 if ( basis < 2 )
43 throw
44 new IllegalArgumentException("Illegaler Wert fuer Basis: " + basis);
45
46 if ( ziffer < 1 || ziffer >= basis )
47 throw
48 new IllegalArgumentException("Illegaler Wert fuer Ziffer: " + ziffer);
49
50 this.ziffer = ziffer;
51 this.basis = basis;
52 }
53
54 public
55 Piep(int ziffer) {
56 this(ziffer,10);
57 }
58
59 public
60 Piep() {
61 this(7);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public
79 boolean piep(int n) {
80 if ( n < 0 )
81 throw
82 new IllegalArgumentException("Illegaler Wert fuer n: " + n);
83
84 return
85 zifferTeilt(n)
86 ||
87 zifferIn(n)
88 ||
89 querSumme(n) == ziffer;
90 }
91
92
93
94 private
95 boolean zifferTeilt(int n) {
96 return
97 n % ziffer == 0;
98 }
99
100
101
102
103
104
105
106
107
108 private
109 boolean zifferIn(int n) {
110 return
111 ( n % basis == ziffer )
112 ||
113 ( ( n >= basis )
114 && zifferIn(n / basis) );
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 private
137 int querSumme(int n) {
138 return
139 ( n < basis )
140 ? n
141 : querSumme(n / basis + n % basis);
142 }
143}
144
145