This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
t_ddemou.pas
109 lines (91 loc) · 2.92 KB
/
t_ddemou.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{***************************************************************
*
* Unit Name: t_ddemou
* Purpose : Delphi MPArith demo program (calculate Mersenne primes)
* Author : W.Ehrhardt Sep.2005
* History : V 0.1 Sep.2005
0.2 Dec.2012 XE3
*
****************************************************************}
unit t_ddemou;
interface
{$i std.inc}
uses
{$ifdef UNIT_SCOPE}
winapi.Windows, winapi.Messages, system.SysUtils,
system.Classes, vcl.Graphics, vcl.Controls, vcl.Forms, vcl.Dialogs,
vcl.StdCtrls, vcl.Buttons, vcl.ExtCtrls,
{$else}
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,
{$endif}
mp_types, mp_numth;
type
TForm1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
BitBtn_Start: TSpeedButton;
BitBtn_Stop: TSpeedButton;
Label_Current: TLabel;
procedure FormShow(Sender: TObject);
procedure BitBtn_StartClick(Sender: TObject);
procedure BitBtn_StopClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
s: integer; //Current index for Mersenne(s)
abort: boolean; //Stop button pressed
running: boolean; //Calculation is already running
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{---------------------------------------------------------------------------}
procedure TForm1.FormShow(Sender: TObject);
{-Show mpint version and program purpose}
begin
Memo1.Lines.Add('Test of MPArith '+ MP_VERSION + ' (c) W.Ehrhardt 2005-2018');
Memo1.Lines.Add('Calculate Mersenne primes using Lucas-Lehmer test');
Memo1.Lines.Add('');
s:=1;
running := false;
end;
{---------------------------------------------------------------------------}
procedure TForm1.BitBtn_StartClick(Sender: TObject);
{-Loop with mp_isMersennePrime, update memo and current index}
begin
if running then exit;
{Set control flags}
running := true;
abort := false;
BitBtn_Start.Enabled := false;
while (s<MaxMersenne) and not abort do begin
Label_Current.Caption := 'Current: '+IntToStr(s);
{Give a chance to update the label and check for stop button}
Application.ProcessMessages;
if abort then break;
if mp_isMersennePrime(s) then begin
Memo1.Lines.Add('Prime: 2^'+IntToStr(s)+'-1');
end;
inc(s);
end;
BitBtn_Start.Enabled := true;
running := false;
end;
{---------------------------------------------------------------------------}
procedure TForm1.BitBtn_StopClick(Sender: TObject);
{-signal abort if stop button clicked}
begin
abort:=true;
end;
{---------------------------------------------------------------------------}
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
{-signal abort if application wants to close}
begin
if running and not abort then CanClose := false;
abort := true;
end;
end.