Beruflich Dokumente
Kultur Dokumente
class Primos
/******************************************************************/
/* La clase Primos define mtodos relacionados con nmeros primos.*/
/* Carlos E. Garca 26feb2016.
*/
/******************************************************************/
{
static void Main()
{
MyApp m = new MyApp();
m.MyPrint(); // Hello World
}
int MCD(int a,int b)
/***********************************************************************/
/* Retorna el MCD de los enteros a y b usando el algoritmo de Euclides.*/
/* Carlos E. Garca 26feb2016.
*/
/***********************************************************************/
{
int r; int Temp;
//Intercambiar si b>a
If (b>a) {Temp=a; a=b; b=Temp}
//Algoritmo Euclides
r=a%b;
while(r!=0)
{
a=b;
b=r;
r=a%b;
}
Return (b);
}
bool Coprimos(int a,int b)
/************************************************/
/* Retorna cierto si n1 y n2 son primo entre s.*/
/* Carlos E. Garca 27feb2016.
*/
/************************************************/
{
Return (MCD(a,b)==1);
}
void Primos(int n)
/**********************************************************/
/* Muestra en pantalla los primeros 10 primos que existen */
/* despus de n. El mximo valor de n es 300,710,281.
*/
/*
*/
/* [De GenPrimos, Ver.B1.2-C.E.Garca/20mar2000 TurboBas].*/
/* Carlos E. Garca 04mar2016.
*/
/**********************************************************/
{
int n=0;
int[] Tbl=new int[2000];
//Inicializar tabla con primeros 1995 primos
GenPrimTbl(Tbl[])
Return (n);
}
Void GenPrimTbl(Int Tbl[])
'************************************************
[Turbo Basic]
'***********************************************************
'* GenPrimo: PROGRAMA GENERADOR DE NUMEROS PRIMOS.
*
'*
Versin B1.2 - C.E.Garca/20mar2000.
*
'***********************************************************
'* Cuando usted especifica un nmero n cualquiera, GENPRI *
'* muestra en pantalla los primeros 10 primos que siguen a *
'* continuacin de n. El mximo valor de n es 300,710,281. *
'***********************************************************
DEFLNG A-Z
DIM Tbl(2000)
DEF FnModul(X,Y)=X-INT(X/Y)*Y
CLS
LOCATE 2,1:PRINT STRING$(80,205):PRINT: PRINT STRING$(80,205)
'Poner nuestra identificacin
Id$="GenPrimo B1.2 - C.E.Garca/20mar2000"
LOCATE 1,1:COLOR 0,7:PRINT Id$: COLOR 7,0
'Inicializar tabla con primeros 1995 primos
CALL GenPrimTbl(Tbl())
Desde=FnDesde
WHILE Desde>0
LOCATE 1,50:COLOR 0,7
PRINT "PRIMOS DESPUES DE:";USING "#,###,###,###";Desde: COLOR 7,0
LOCATE 3,1: PRINT STRING$(80,32)
LOCATE 4,1: PRINT STRING$(80,32)
LOCATE 3,1
IF Desde<=17239 THEN
CALL GenFrmTbl(Desde,Tbl())
ELSE
CALL GenFrmCal(Desde,Tbl())
END IF
Desde=FnDesde
WEND
STOP
'********************************************************
'* Listador de primos entre 1/17341. (directo de tabla) *
'********************************************************
SUB GenFrmTbl(Desde,Tbl(1))
LOCAL I,Ult,Ini!,Fin!,PrimoList()
DIM PrimoList(10)
Ini!=TIMER
!-> Single-precision floating-point variable
I=1
WHILE Tbl(I)<Desde
INCR I
WEND
Ult=I
FOR I=Ult TO Ult+9
PrimoList(I-Ult+1)=Tbl(I)
NEXT I
Fin!=TIMER
CALL List10(PrimoList(),Fin!-Ini!)
END SUB
'****************************************************
'* Generador de nmeros primos de 17341 en adelante *
'****************************************************
SUB GenFrmCal(Desde,Tbl(1))
LOCAL I,J,Cierto,Falso,Primo,Hasta,Ult,Ini!,Fin!,Max,NPrim,PrimoList()
DIM PrimoList(10)
Ini!=TIMER
Cierto=1: Falso=0
IF FnModul(Desde,2)=0 THEN INCR Desde
Hasta=Desde+300
IF Hasta>300710200 THEN Hasta=300710200
Max=SQR(Hasta)
NPrim=0
FOR I=Desde TO Hasta STEP 2
Primo=Cierto
FOR J=3 TO 1994
IF FnModul(I,Tbl(J))=0 THEN
Primo=Falso: J=1995
ELSE
IF Tbl(J)>Max THEN J=1995
END IF
NEXT J
IF Primo THEN INCR NPrim: PrimoList(NPrim)=I
IF NPrim>=10 THEN I=Hasta+1
NEXT I
Fin!=TIMER
CALL List10(PrimoList(),Fin!-Ini!)
END SUB
'******************************************************
'* Generador de tabla de nmeros primos entre 1/17350 *
'******************************************************
SUB GenPrimTbl(Tbl(1))
LOCAL I,J,Ptr,Max,Primo,Cierto,Falso,Ini!,Fin!,Segs!
Ini!=TIMER
Cierto=1: Falso=0
Tbl(1)=1: Tbl(2)=2: Tbl(3)=3: Ptr=3
FOR I=5 TO 17350 STEP 2
Primo=Cierto
Max=SQR(I)
FOR J=3 TO Ptr
IF (I MOD Tbl(J))=0 THEN Primo=Falso: J=Ptr+1
IF Tbl(J)>Max THEN J=Ptr+1
NEXT J
IF Primo THEN INCR Ptr: Tbl(Ptr)=I
NEXT I
Fin!=TIMER
Segs!=Fin!-Ini!
LOCATE 1,38:COLOR 0,7:PRINT "Segs:";USING "###.##";Segs!:COLOR 7,0
END SUB
'********************
'* Listar 10 primos *
'********************
SUB List10(Primo(1),Segs!)
LOCAL I%
FOR I%=1 TO 5
PRINT USING " #,###,###,###";Primo(I%);
NEXT I%
PRINT
FOR I%=6 TO 10
PRINT USING " #,###,###,###";Primo(I%);
NEXT I%
LOCATE 1,38:COLOR 0,7:PRINT "Segs:";USING "###.##";Segs!:COLOR 7,0
END SUB
'*********************