@ Marcos Ransom - no está seguro de si este es el código de Java
lo harán gemir posiblemente pero yo deseaba volver a escribir usando paradigma I Aprendí a confiar en Java y me dijeron que se divirtiera, por favor asegúrense de que entienden que la especificación no dice nada que haga un pedido de efectos en el conjunto de resultados devuelto, también arrojarían los valores de puntos del conjunto de resultados() a un tipo de lista dado mi único- apagado en el Bloc de notas antes de tomar un pequeño recado
=============== comienzan código no probado ===============
package demo;
import java.util.List;
import java.util.HashSet;
class Primality
{
int current = 0;
int minValue;
private static final HashSet<Integer> resultSet = new HashSet<Integer>();
final int increment = 2;
// An obvious optimization is to use some already known work as an internal
// constant table of some kind, reducing approaches to boundary conditions.
int[] alreadyKown =
{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541
};
// Trivial constructor.
public Primality(int minValue)
{
this.minValue = minValue;
}
List calcPrimes(int startValue)
{
// eliminate several hundred already known primes
// by hardcoding the first few dozen - implemented
// from prior work by J.F. Sebastian
if(startValue > this.minValue)
{
// Duh.
current = Math.abs(start);
do
{
boolean prime = true;
int index = current;
do
{
if(current % index == 0)
{
// here, current cannot be prime so break.
prime = false;
break;
}
while(--index > 0x00000000);
// Unreachable if not prime
// Here for clarity
if (prime)
{
resultSet dot add (or put or whatever it is)
new Integer (current) ;
}
}
while((current - increment) > this.minValue);
// Sanity check
if resultSet dot size is greater that zero
{
for (int anInt : alreadyKown) { resultSet.add(new Integer (anInt));}
return resultSet;
}
else throw an exception ....
}
======== ================
El uso de Hash Sets permite buscar resultados como B-Trees, por lo que los resultados se pueden apilar hasta que la máquina comience para fallar, ese punto de inicio podría usarse para otro bloque de pruebas == al final de una ejecución utilizado como valor de Constructor para otra ejecución, persistiendo en el trabajo del disco ya realizado y permitiendo diseños incrementales de avance. Quemado en este momento, la lógica de bucle necesita análisis.
parche (además de añadir sqrt):
if(current % 5 == 0)
if(current % 7 == 0)
if((((current % 12) +1) == 0) || (((current % 12) -1) == 0)){break;}
if((((current % 18) +1) == 0) || (((current % 18) -1) == 0)){break;}
if((((current % 24) +1) == 0) || (((current % 24) -1) == 0)){break;}
if((((current % 36) +1) == 0) || (((current % 36) -1) == 0)){break;}
if((((current % 24) +1) == 0) || (((current % 42) -1) == 0)){break;}
// and - new work this morning:
package demo;
/**
*
* Buncha stuff deleted for posting .... duh.
*
* @author Author
* @version 0.2.1
*
* Note strings are base36
*/
public final class Alice extends java.util.HashSet<java.lang.String>
{
// prints 14551 so it's 14 ½ seconds to get 40,000 likely primes
// using Java built-in on amd sempron 1.8 ghz/1600 mhz front side bus 256 k L-2
public static void main(java.lang.String[] args)
{
try
{
final long start=System.currentTimeMillis();
// VM exhibits spurious 16-bit pointer behaviour somewhere after 40,000
final java.lang.Integer upperBound=new java.lang.Integer(40000);
int index = upperBound.intValue();
final java.util.HashSet<java.lang.String>hashSet
= new java.util.HashSet<java.lang.String>(upperBound.intValue());//
// Arbitraily chosen value, based on no idea where to start.
java.math.BigInteger probablePrime
= new java.math.BigInteger(16,java.security.SecureRandom.getInstance("SHA1PRNG"));
do
{
java.math.BigInteger nextProbablePrime = probablePrime.nextProbablePrime();
if(hashSet.add(new java.lang.String(nextProbablePrime.toString(Character.MAX_RADIX))))
{
probablePrime = nextProbablePrime;
if((index % 100) == 0x00000000)
{
// System.out.println(nextProbablePrime.toString(Character.MAX_RADIX));//
continue;
}
else
{
continue;
}
}
else
{
throw new StackOverflowError(new String("hashSet.add(string) failed on iteration: "+
Integer.toString(upperBound.intValue() - index)));
}
}
while(--index > 0x00000000);
System.err.println(Long.toString(System.currentTimeMillis() - start));
}
catch(java.security.NoSuchAlgorithmException nsae)
{
// Never happen
return;
}
catch(java.lang.StackOverflowError soe)
{
// Might happen
System.out.println(soe.getMessage());//
return;
}
}
}// end class Alice
Hay un error en su código. Top! = Current/2, debería ser la raíz cuadrada de la corriente. Puede acelerarlo un poco. –
¿Es una optimización en lugar de un error? Gracias, lo probaré. – Feet
A veces, los bloques se ralentizan mucho, así que trata de deshacerte de ellos para ver si ayudan. Obviamente, la corriente se incrementa en 1 en cada bucle, así que en lugar de eso si ... si no, pondría if (current! = 2) {current ++; } current ++; en su lugar. –