Sie sind auf Seite 1von 6

using System;

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[])
'************************************************

'* Genera tabla de nmeros primos entre 1/17350 *


'***************************************************
SUB GenPrimTbl(Tbl(1))
{
int I,J,Ptr,Max,Primo;
float Ini,Fin,Segs;
bool EsPrimo;
//Anotar hora de inicio
DateTime Ini=DateTime.Now;
Tbl[1]=1; Tbl[2]=2; Tbl[3]=3; Ptr=3;
FOR (I=5;I,<=17350;J+=2)
{
EsPrimo=true;
Max=Math.Sqrt(I);
FOR (J=3;J<=Ptr;J++)
{
IF (I%Tbl(J)=0 then {EsPrimo=false; J=Ptr+1}
IF Tbl(J)>Max then J=Ptr+1;
}
IF EsPrimo then {Ptr+; Tbl(Ptr)=I}
}
//Anotar hora de finalizacin
DateTime Fin=DateTime.Now;
//Poner en pantalla tiempo de proceso
Segs=Fin-Ini;
Console.WriteLine("Segs: "+Segs); //System.Console.WriteLine
}
Int NumDesde();
/*************************************************************************/
/* Solicitar al usuario el nmero a partir del cual quiere listar primos */
/* y retornar este nmero.
*/
'*************************************************************************/
{
Int Desde;
Console.Write("Mostrar los 10 primos despus de: ");
Desde=Convert.ToInt64(Console.ReadLine());
//Also: age=long.Parse(Console.ReadLine());
//Validar
WHILE (Desde<0|Desde>300710281)
{
Console.WriteLine();
Console.WriteLine("INVALIDO: El nmero debe estar entre 1 y 300,710,281);
Console.Write("Mostrar los 10 primos despus de: ");
Desde=Console.ReadLine();
}
Return (Desde);
}
Void List10(long[],int Segs);
'********************
'* Listar 10 primos *
'********************
Int I;
Console.WriteLine(Los 10 primos son:);
FOR (I=1,I<=10,I++)
Console.WriteLine(Primo[I];

//PRINT USING " #,###,###,###";Primo(I%);


Console.WriteLine();
Console.WriteLine(Tiempo: ,Segs);
//LOCATE 1,38:COLOR 0,7:PRINT "Segs:";USING "###.##";Segs!:COLOR 7,0
}
Void GenFromCalc(int Desde,long Tbl[])
'***********************************************
'* Genera nmeros primos de 17341 en adelante. *
'***********************************************
SUB GenFrmCal(Desde,Tbl(1))
int I,J,Cierto,Falso,Primo,Hasta,Ult,Max,NPrim
float Ini,Fin
int[10] 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

}//FIN Clase Primos

[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
'*********************

'* Desde qu numero? *


'*********************
DEF FNDesde
LOCAL Desde
Desde=-1
LOCATE 6,17:PRINT "
"
WHILE Desde<0 OR Desde>300710281
BEEP:LOCATE 6,1:INPUT "10 primos despus de: ",Desde
WEND
FNDesde=Desde
END DEF

Das könnte Ihnen auch gefallen