Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Additive-primes/Processing/additive-primes.processing
Normal file
36
Task/Additive-primes/Processing/additive-primes.processing
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
IntList primes = new IntList();
|
||||
|
||||
void setup() {
|
||||
sieve(500);
|
||||
int count = 0;
|
||||
for (int i = 2; i < 500; i++) {
|
||||
if (primes.hasValue(i) && primes.hasValue(sumDigits(i))) {
|
||||
print(i + " ");
|
||||
count++;
|
||||
}
|
||||
}
|
||||
println();
|
||||
print("Number of additive primes less than 500: " + count);
|
||||
}
|
||||
|
||||
int sumDigits(int n) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i <= floor(log(n) / log(10)); i++) {
|
||||
sum += floor(n / pow(10, i)) % 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
void sieve(int max) {
|
||||
for (int i = 2; i <= max; i++) {
|
||||
primes.append(i);
|
||||
}
|
||||
for (int i = 0; i < primes.size(); i++) {
|
||||
for (int j = i + 1; j < primes.size(); j++) {
|
||||
if (primes.get(j) % primes.get(i) == 0) {
|
||||
primes.remove(j);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue