Ad
Puzzles
Mathematics
Code
Diff
  • import java.util.Iterator;
    import java.util.TreeSet;
    class AncientPrimes { 
    	public static Integer[] sumOfPrimes(int start, int end) {
    		TreeSet<Integer> primes = new TreeSet<Integer>();
    		TreeSet<Integer> result = new TreeSet<Integer>();
    		boolean[] table = new boolean[end + 1];
    		table[0] = true;
    		table[1] = true;
    
    		for (int i = start; i < table.length; i++) {
    			if (!table[i]) {
    				for (int j = i + i; j < table.length; j += i) {
    					table[j] = true;
    				}
    				int minusResult = Integer.MIN_VALUE;
    				int minus = 0;
    				Iterator<Integer> iterator = primes.descendingIterator();
    				boolean isPrime = false;
    				while (minusResult < minus && iterator.hasNext() && !isPrime) {
    					minus = iterator.next();
    					minusResult = i - minus;
    					isPrime = primes.contains(minusResult);
    				}
    
    				if (isPrime)
    					result.add(i);
    
    				primes.add(i);
    			}
    		}
    		return result.toArray(new Integer[0]);
    	}
    }
    • import java.util.Iterator;
    • import java.util.TreeSet;
    • class AncientPrimes {
    • class AncientPrimes {
    • public static Integer[] sumOfPrimes(int start, int end) {
    • TreeSet<Integer> primes = new TreeSet<Integer>();
    • TreeSet<Integer> result = new TreeSet<Integer>();
    • boolean[] table = new boolean[end + 1];
    • table[0] = true;
    • table[1] = true;
    • for (int i = start; i < table.length; i++) {
    • if (!table[i]) {
    • for (int j = i + i; j < table.length; j += i) {
    • table[j] = true;
    • }
    • int minusResult = Integer.MIN_VALUE;
    • int minus = 0;
    • Iterator<Integer> iterator = primes.descendingIterator();
    • boolean isPrime = false;
    • while (minusResult < minus && iterator.hasNext() && !isPrime) {
    • minus = iterator.next();
    • minusResult = i - minus;
    • isPrime = primes.contains(minusResult);
    • }
    • if (isPrime)
    • result.add(i);
    • primes.add(i);
    • }
    • }
    • return result.toArray(new Integer[0]);
    • }
    • }
Puzzles
Mathematics

The Ancient Civilitation

Once upon a time, there was an ancient civilization that devoted itself to studying prime numbers. Among their studies, they found an interesting property that consisted of decomposing a prime number into the sum of two prime numbers. However, this civilization established a very particular rule: they considered the provided number and the next one always as prime numbers, but all numbers smaller than the provided one were not.

This rule became a mystery for many people who tried to solve it, but the civilization kept its secret well-guarded for centuries. Finally, one day, a wise mathematician managed to discover the enigma and found that the result of this property was a very particular set of prime numbers, which had been hidden by the civilization to keep their knowledge secret. Since then, this discovery has become a valuable tool for solving many mathematical problems and even today, it remains a subject of research and study for many mathematicians.

Description

The result is the set of prime numbers that can be decomposed from the sum of two prime numbers, considering the provided number and the next one as primes, and all numbers smaller than the provided one are not.

AncientPrimes.sumOfPrimes(3, 50); // => [7, 11, 17, 23, 41, 47]

AncientPrimes.sumOfPrimes(10, 50); // => [21, 23, 25, 27, 29, 31, 35, 37, 41, 43, 47, 49]

AncientPrimes.sumOfPrimes(20, 10); // => []
class AncientPrimes {
  public static Integer[] sumOfPrimes (int start, int end){
    // To Do
    return new Integer[0];
  }
}