Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Neuen Beitrag zur Code-Library hinzufügen (https://www.delphipraxis.net/33-neuen-beitrag-zur-code-library-hinzufuegen/)
-   -   Beliebig lange Dezimalstellen ab einer beliebigen Stelle... (https://www.delphipraxis.net/155256-beliebig-lange-dezimalstellen-ab-einer-beliebigen-stelle.html)

Aphton 14. Okt 2010 21:43

Beliebig lange Dezimalstellen ab einer beliebigen Stelle...
 
Hier ist mal ein kleiner Codesnippet, welchen ich verfasst habe, um diverse Programmieraufgaben (beispielsweise die hier) zu lösen.

Code:
(Aufgabe, falls die Seite irgendwann einmal down gehen sollte)
Infinite division

Calculate 13155187 / 13417 with 5000 decimals.
The solution is the last 6 numbers (of those 5000 decimals).
Delphi-Quellcode:
type
  TByteArray = Array of Byte;

function GetDecimalValues(Dividend: Integer; const Divisor: Integer; From: DWord; const Digits: DWord): TByteArray;
var
  BeforeComma: Boolean;
  Remainder: Integer;
  Pos, i: Integer;
begin
  if From = 0 then
    From := 1;
  if Digits = 0 then
    Exit;
  SetLength( Result, Digits );
  BeforeComma := True;
  repeat
    Remainder := Dividend mod Divisor;
    if (Remainder < Divisor) and (BeforeComma) then
    begin
      BeforeComma := False;
      Pos := 0;
    end;
    i := Pos - From + 1;
    if (i < Digits) and (i >= 0) then
      Result[i] := ( Remainder * 10 ) div Divisor;
    Dividend := Remainder * 10;
    if not BeforeComma then
      inc( Pos );
  until Pos >= From + Digits - 1;
end;

// Möglicher Aufruf:
var
  D: TByteArray;
begin
  { 113 / 35 = 3,228571428571428571428... }
  D := GetDecimalValues( 113, 35, 1, 4 );
// D = [2,2,8,5]
end;
MfG,
Aphton


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:27 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz