Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Promedio.. digitos primos .. (https://www.clubdelphi.com/foros/showthread.php?t=73644)

luisito2011 06-05-2011 22:35:00

Promedio.. digitos primos ..
 
ola.. saludos a todos..
bueno no se si se ban a reir...
la verdad que no recuerdo como ver si un digito es numero primo :mad::confused:
necesito sacar
// el promedio de digitos.. de N.. numero.. ingresado por teclado//
ej-- 1731--
prom:= 5

teniendo.. estas 3 functiones
de clase// cnumeronatural =class //
Código Delphi [-]
 
Function  CNumeroNatural.Digito( Posicion : Byte ) : Byte;
Var
   Aux : Cardinal;
   Digi , Digito : Byte;
Begin
     Aux := Valor;
     Digi := 0;
     if( Posicion > 0 )and( Posicion <= NumeroDigitos )then
     Begin
          Repeat
             Digito := Aux Mod 10;
             Aux := Aux Div 10;
             Inc( Digi );
          Until( Digi > NumeroDigitos-Posicion );
          Result := Digito;
     End
     Else
         Raise CEMNumeroNatural.Create
               ('CNumeroNatural.Digito: Error Fuera de RANGO...');
End;      
 
Function  CNumeroNatural.NumeroDigitos : Byte;
Var
   Aux : Cardinal;
   Cant : Byte;
Begin
     Aux := Valor;
     Cant := 0;
     Repeat
        Aux := Aux Div 10;
        Inc( Cant );
     Until(Aux = 0);
     Result := Cant;
End;
Function  CNumeroNatural.ObtenerValor : Cardinal;
Begin
     Result := Valor;
End;
 
Procedure CNumeroNatural.AsignarValor( NuevoValor : Cardinal);
Begin
     Valor := NuevoValor;
End;

bye

Casimiro Notevi 06-05-2011 22:39:37

Esa función te dice si un número es primo o no.

Código Delphi [-]
function esPrimo(x : integer) : boolean;
var
  i,r : longint;
begin
  r:=round(sqrt(x));
  for i:=2 to r do
    if (x mod i=0) then
    begin
      result:=false;
      exit;
    end;
  result:=true;
end;

Ahora tendrás que adaptarla a tu código ;)

luisito2011 07-05-2011 02:46:35

mmm bueno.. eso ya lo savia solo que esto...

Cita:

r:=round(sqrt(x));

sqrt.. es raiz cuadrada..
y Round ?.. para que sirve

ecfisa 07-05-2011 02:54:02

Hola.

La ayuda de Delphi (F1) dice:
Cita:

Returns the value of X rounded to the nearest whole number.

Unit
System

Category
arithmetic routines

Delphi syntax
function Round(X: Extended): Int64;

Description
In Delphi, the Round function rounds a real-type value to an integer-type value.

X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number. This method of rounding is often called "Banker’s Rounding".

If the rounded value of X is not within the Int64 range, a runtime error is generated, which can be handled using the EInvalidOp exception.

Note:The behavior of Round can be affected by the Set8087CW procedure or SetRoundMode function.
Es decir que devuelve el valor de x(real), redondeado al número entero más próximo, necesario en ese caso, por ser 'r' una variable de tipo entera.

Saludos.


La franja horaria es GMT +2. Ahora son las 12:24:17.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi