![]() |
Re: Mathe Problem 1+2-3 ... +99-100
Zitat:
|
Re: Mathe Problem 1+2-3 ... +99-100
Jetzt noch ein Lösungsvorschlag von mir :mrgreen: :
Man errechnet die Summe der positiven und die der negativen Zahlen mit der Gaußschen Summenformel und subtrahiert sie dann. Sollte langsamer als die "Pakete"-Idee sein, dafür kann man alles schnell von 1+2-3+4... auf 1-2+3-4... oder auch 2-3+4-6+6-9... umstellen.
Delphi-Quellcode:
function Foo: Integer;
// Erste Zahl, Letzte Zahl, Anzahl function Gauss(AFrom, ATo, ACount: Integer): Integer; begin Result := (AFrom + ATo) * ((ATo - AFrom) div ACount + 1) div 2; // = (From + To) * Steps / 2 // Steps = (From - To) div Count + 1 end; begin Result := Gauss(1, 99, 2) - Gauss(2, 100, 2); //1 - 2 + 3... // Result := Gauss(2, 50 * 2, 2) - Gauss(3, 50 * 3, 3); //2 - 3 + 4 - 6..., 50 Paare end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:47 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz