Chowla numbers: Difference between revisions

Content added Content deleted
(add Java implementation)
(define methods)
Line 1,575: Line 1,575:


public static void main(String[] args) {
public static void main(String[] args) {
int limit = 37;
int[] chowlaNumbers = findChowlaNumbers(37);
for (int i = 1; i <= limit; i++) {
for (int i = 0; i < chowlaNumbers.length; i++) {
System.out.printf("chowla(%d) = %d%n", i, chowla(i));
System.out.printf("chowla(%d) = %d%n", (i+1), chowlaNumbers[i]);
}
}
System.out.println();
System.out.println();


int n, count = 0, power = 100;
int[][] primes = countPrimes(100, 10_000_000);
limit = 10000000;
for (int i = 0; i < primes.length; i++) {
System.out.printf(Locale.US, "There is %,d primes up to %,d%n", primes[i][1], primes[i][0]);
for (n = 2; n <= limit; n++) {
}
System.out.println();

int[] perfectNumbers = findPerfectNumbers(35_000_000);
for (int i = 0; i < perfectNumbers.length; i++) {
System.out.printf("%d is a perfect number%n", perfectNumbers[i]);
}
System.out.printf(Locale.US, "There are %d perfect numbers < %,d%n", perfectNumbers.length, 35_000_000);
}

public static int chowla(int n) {
if (n < 0) throw new IllegalArgumentException("n is not positive");
int sum = 0;
for (int i = 2, j; i * i <= n; i++)
if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
return sum;
}

protected static int[][] countPrimes(int power, int limit) {
int count = 0;
int[][] num = new int[countMultiplicity(limit, power)][2];
for (int n = 2, i=0; n <= limit; n++) {
if (chowla(n) == 0) count++;
if (chowla(n) == 0) count++;
if (n % power == 0) {
if (n % power == 0) {
System.out.printf(Locale.US, "There is %,d primes up to %,d%n", count, power);
num[i][0] = power;
num[i][1] = count;
i++;
power *= 10;
power *= 10;
}
}
}
}
System.out.println();
return num;
}

protected static int countMultiplicity(int limit, int start) {
int count = 0;
int cur = limit;
while(cur >= start) {
count++;
cur = cur/10;
}
return count;
}

protected static int[] findChowlaNumbers(int limit) {
int[] num = new int[limit];
for (int i = 0; i < limit; i++) {
num[i] = chowla(i+1);
}
return num;
}

protected static int[] findPerfectNumbers(int limit) {
int count = 0;
int[] num = new int[count];


count = 0;
limit = 35000000;
int k = 2, kk = 3, p;
int k = 2, kk = 3, p;
while ((p = k * kk) < limit) {
while ((p = k * kk) < limit) {
if (chowla(p) == p - 1) {
if (chowla(p) == p - 1) {
System.out.printf("%d is a perfect number%n", p);
num = increaseArr(num);
count++;
num[count++] = p;
}
}
k = kk + 1;
k = kk + 1;
kk += k;
kk += k;
}
}
return num;
System.out.printf(Locale.US, "There are %d perfect numbers < %,d%n", count, limit);
}
}


public static int chowla(int n) {
private static int[] increaseArr(int[] arr) {
if (n < 0) throw new IllegalArgumentException("n is not positive");
int[] tmp = new int[arr.length + 1];
int sum = 0;
System.arraycopy(arr, 0, tmp, 0, arr.length);
for (int i = 2, j; i * i <= n; i++)
return tmp;
if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
return sum;
}
}
}
}