Einzelnen Beitrag anzeigen

Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#5

Re: Prüfen ob Potenz von 2

  Alt 2. Mär 2008, 14:49
Hi,

wir wärs mit Mathe?

Ceil(log 2 (Zahl)) = log 2 (Zahl)

(Also man muss prüfen ob log 2 (Zahl) ne Ganzzahl ist)

PS:

Folgendes funktioniert bei mir soweit ich das getestet habe:

Delphi-Quellcode:
uses Math;

function Is2erPotenz(Zahl: Integer): Boolean;
var tmp: Single;
begin
  tmp := ln(Zahl) / ln(2);
  Result := tmp = Ceil(tmp);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Is2erPotenz(StrToInt(Edit1.Text)) then
    ShowMessage('Potenz von 2!');
end;
Gruß
Neutral General
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat