Buscar

Classe Java -Testa, Gera e Manipula Numeros Primos Giagantes , Gera os Primos de Mersenne

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

Primos/build.xml
 
 Builds, tests, and runs the project Primos.
 
 
Primos/build/built-jar.properties
#Mon, 25 Apr 2016 01:19:28 -0300
/home/joao/NetBeansProjects/Primos=
Primos/build/classes/list/Node.class
Primos/build/classes/list/PrimeList.class
Primos/build/classes/primos/BigPrime.class
Primos/build/classes/primos/Primos.class
Primos/dist/Primos.jar
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_77-b03 (Oracle Corporation)
Class-Path: 
X-COMMENT: Main-Class will be added automatically by build
Main-Class: primos.Primos
list/Node.class
list/PrimeList.class
primos/BigPrime.class
primos/Primos.class
Primos/dist/README.TXT
========================
BUILD OUTPUT DESCRIPTION
========================
When you build an Java application project that has a main class, the IDE
automatically copies all of the JAR
files on the projects classpath to your projects dist/lib folder. The IDE
also adds each of the JAR files to the Class-Path element in the application
JAR files manifest file (MANIFEST.MF).
To run the project from the command line, go to the dist folder and
type the following:
java -jar "Primos.jar" 
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
Notes:
* If two JAR files on the project classpath have the same name, only the first
JAR file is copied to the lib folder.
* Only JAR files are copied to the lib folder.
If the classpath contains other types of files or folders, these files (folders)
are not copied.
* If a library on the projects classpath also has a Class-Path element
specified in the manifest,the content of the Class-Path element has to be on
the projects runtime path.
* To set a main class in a standard Java project, right-click the project node
in the Projects window and choose Properties. Then click Run and enter the
class name in the Main Class field. Alternatively, you can manually type the
class name in the manifest Main-Class element.
Primos/manifest.mf
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Primos/nbproject/build-impl.xml
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set src.dir
 Must set test.src.dir
 Must set build.dir
 Must set dist.dir
 Must set build.classes.dir
 Must set dist.javadoc.dir
 Must set build.test.classes.dir
 Must set build.test.results.dir
 Must set build.classes.excludes
 Must set dist.jar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set javac.includes
No tests executed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must set JVM to use for profiling in profiler.info.jvm
 Must set profiler agent JVM arguments in profiler.info.jvmargs.agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 To run this application from the command line without
Ant, try:
 
 java -jar "${dist.jar.resolved}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must select one file in the IDE or set debug.class
 
 
 
 
 Must set fix.includes
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set profile.class
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 This target only works when run from inside the NetBeans IDE.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 
 Must select one file in the IDE or set run.class
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set javac.includes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Some tests failed; see details above.
 
 
 
 
 
 
 
 
 Must select some files in the IDE or set test.includes
 
 
 
 Some tests failed; see details above.
 
 
 
 Must select some files in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 Some tests failed; see details above.
 
 
 
 
 Must select one file in the IDE or set test.class
 
 
 
 Must select one file in the IDE or set test.class
 Must select some method in the IDE or set test.method
 
 
 
 
 
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 Must select one file in the IDE or set applet.url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Primos/nbproject/genfiles.properties
build.xml.data.CRC32=810c0209
build.xml.script.CRC32=146114bb
build.xml.stylesheet.CRC32=8064a381@1.75.2.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=810c0209
nbproject/build-impl.xml.script.CRC32=dcb603e0
nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48
Primos/nbproject/private/private.properties
compile.on.save=true
user.properties.file=/home/joao/.netbeans/8.0.2/build.properties
Primos/nbproject/private/private.xml
 
 
 
 
 file:/home/joao/NetBeansProjects/Primos/src/primos/Primos.java
 file:/home/joao/NetBeansProjects/Primos/src/primos/BigPrime.java
 
 
Primos/nbproject/project.properties
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
 ${run.classpath}
debug.test.classpath=\
 ${run.test.classpath}
# Os arquivos em build.classes.dir que devem ser exclu\u00eddos do jar de distribui\u00e7\u00e3o
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/Primos.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.processorpath=\
 ${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
javac.test.processorpath=\
 ${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=primos.Primos
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
 ${javac.classpath}:\
 ${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
 ${javac.test.classpath}:\
 ${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
Primos/nbproject/project.xml
 
 org.netbeans.modules.java.j2seproject
 
 
 Primos
 
 
 
 
 
 
 
 
Primos/src/list/Node.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package list;
import java.math.BigInteger;
/**
 *
 * @author joao
 */
public class Node {
 private BigInteger num;
 private Node ant,prox;
 public Node() {
 this.ant=this.prox=null;
 num=new BigInteger("0");
 }
 
 
 public Node(BigInteger num) {
 this.num = num;
 }
 public BigInteger getNum() {
 return num;
 }
 public void setNum(BigInteger num) {
 this.num = num;
 }
 public Node getAnt() {
 return ant;
 }
 public void setAnt(Node ant) {
 this.ant = ant;
 }
 public Node getProx() {
 return prox;
 }
 public void setProx(Node prox) {
 this.prox = prox;
 }
 
}
Primos/src/list/PrimeList.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package list;
import java.math.BigInteger;
/**
 *
 * @author joao
 */
public class PrimeList {
 private Node ini;
 private Node fim;
 public PrimeList() {
 this.ini = this.fim = null;
 }
 //--------------------------------------------------------------------------
 public void init() {
 this.ini = null;
 this.fim = null;
 }
 //--------------------------------------------------------------------------
 public void insereFinal(BigInteger num) {
 if (ini == null) {
 ini = fim = new Node(num);
 } else {
 fim.setProx(new Node(num));
 fim.getProx().setAnt(fim);
 fim = fim.getProx();
 }
 }
 //--------------------------------------------------------------------------
 public void push(BigInteger num) {
 if (ini == null) {
 ini = fim = new Node(num);
 } else {
 ini.setAnt(new Node(num));
 ini.getAnt().setProx(ini);
 ini = ini.getAnt();
 }
 }
 //--------------------------------------------------------------------------
 public Node buscaExaustiva(BigInteger chave) {
 Node aux = ini;
 while (aux != null && aux.getNum().compareTo(chave) != 0) {
 aux = aux.getProx();
 }
 return aux;
 }
 //--------------------------------------------------------------------------
 private Node buscaSentinela(BigInteger chave) {
 insereFinal(chave);
 Node aux = ini;
 while (aux.getNum().compareTo(chave) != 0) {
 aux = aux.getProx();
 }
 return (aux.getProx() != null) ? aux : null;
 }
 //--------------------------------------------------------------------------
 public Node buscaExaustivaS(BigInteger chave) {
 Node aux = buscaSentinela(chave);
 fim = fim.getAnt();
 fim.setProx(null);
 return aux;
 }
 //--------------------------------------------------------------------------
 public void remove(BigInteger info) {
 Node aux = buscaExaustivaS(info);
 if (aux != null) {
 if (ini == fim) {
 init();
 } else if (aux == ini) {
 ini = ini.getProx();
 ini.setAnt(null);
 } else if (aux == fim) {
 fim = aux.getAnt();
 fim.setProx(null);
 } else {
 aux.getAnt().setProx(aux.getProx());
 aux.getProx().setAnt(aux.getAnt());
 }
 }
 }
 //--------------------------------------------------------------------------
 public void insercaoDireta() {
 Node pos, i = ini.getProx();
 BigInteger aux;
 while (i != null) {
 aux = i.getNum();
 pos = i;
 while (pos.getAnt() != null && aux.compareTo(pos.getAnt().getNum()) < 0) {
 pos.setNum(pos.getAnt().getNum());
 pos = pos.getAnt();
 }
 pos.setNum(aux);
 i = i.getProx();
 }
 }
 //--------------------------------------------------------------------------
 public void selecaoDireta() {
 BigInteger menor;
 Node i = ini, j;
 while (i.getProx() != null) {
 menor = i.getNum();
 j = i.getProx();
 while (j != null) {
 if (j.getNum().compareTo(menor) < 0) {
 menor = j.getNum();
 }
 j.setNum(i.getNum());
 i.setNum(menor);
 j = j.getProx();
 }
 i = i.getProx();
 }
 }
 //--------------------------------------------------------------------------
 public void bubbleSort() {
 Node i = ini, tl2 = fim;
 BigInteger aux;
 while (tl2 != null) {
 while (i.getProx() != null) {
 if (i.getNum().compareTo(i.getProx().getNum())>0) {
 aux = i.getNum();
 i.setNum(i.getProx().getNum());
 i.getProx().setNum(aux);
 }
 i = i.getProx();
 }
 i = ini;
 tl2 = tl2.getAnt();
 }
 }
}
Primos/src/primos/BigPrime.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package primos;
import java.math.BigInteger;
/**
 *
 * @author joao
 */
public class BigPrime {
 private BigInteger num;
 public BigPrime() {
 this.num = BigInteger.ZERO;
 }
 public BigInteger getNum() {
 return num;
 }
 public void setNum(BigInteger num) {
 this.num = num;
 }
 public void setNum(int num) {
 this.num = BigInteger.valueOf(num);
 }
 public void plusOne() {
 this.num = this.num.add(BigInteger.ONE);
 }
 public boolean isPrime(BigInteger n) {
 BigInteger div = BigInteger.valueOf(3);
 BigInteger dAux = BigInteger.valueOf(2);
 boolean isPrime = true;
 /* se n <= 1 ou par > 2 nao e' primo */
 isPrime = !(n.compareTo(BigInteger.ONE) <= 0 || (n.compareTo(dAux) != 0 && n.mod(dAux).compareTo(BigInteger.ZERO) == 0));
 while (isPrime && div.compareTo(n.divide(dAux)) <= 0) {
 if ((n.mod(div)).compareTo(BigInteger.ZERO) == 0) {
 isPrime = false;
 }
 div = div.add(dAux);/* somente os impares sao testados 3,5,7,9...*/
 }
 return isPrime;
 }
 BigInteger getMersennePrime(BigInteger n){
 BigInteger auxEx=BigInteger.valueOf(2);
 BigInteger aux=BigInteger.ZERO;
 if(isPrime(n)){
 aux=auxEx.pow(n.intValue()).subtract(BigInteger.ONE);
 
 return (isPrime(aux))?aux:null;
}
 return null;
 }
}
Primos/src/primos/Primos.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package primos;
import java.math.BigInteger;
/**
 *
 * @author joao
 */
public class Primos {
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 // TODO code application logic here
 BigPrime pr = new BigPrime();
 BigInteger limite, aux;
 double tIni, tFim = 0;
 int cont = 0,nM=1;
 
 limite = new BigInteger("100");
 System.out.println("Inicio");
 System.out.println(limite.toString());
 tIni = System.currentTimeMillis();
 for (BigInteger i = BigInteger.ZERO; i.compareTo(limite) < 0; i = i.add(BigInteger.ONE)) {
 aux=pr.getMersennePrime(i);
 if(aux!=null){
 System.out.println("#["+nM+++"] n["+cont+"] Mersenne["+aux.toString()+"] Tempo:" + ((System.currentTimeMillis()-tIni) / 1000.0)+"");
 }
 cont++;
 }
 tFim = System.currentTimeMillis();
 System.out.println("Final total [" + cont + "]");
 System.out.println("Tempo Total:" + (tFim - tIni) / 1000.0);
 }
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais