-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathunit1.pas
More file actions
133 lines (115 loc) · 4.4 KB
/
unit1.pas
File metadata and controls
133 lines (115 loc) · 4.4 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
(******************************************************************************)
(* Prime factorizer 21.06.2024 *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : calc the prime factors of a given number *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Memo1: TMemo;
Procedure Button1Click(Sender: TObject);
Procedure Edit1KeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
Procedure FormCreate(Sender: TObject);
private
public
End;
Var
Form1: TForm1;
Implementation
{$R *.lfm}
Uses LCLType, math;
{ TForm1 }
Procedure TForm1.Button1Click(Sender: TObject);
Var
sqrtn, n, i, j: uint64;
s: String;
Begin
Memo1.Clear;
// Load the requested number and a bit of error handling ;)
edit1.text := trim(edit1.text);
n := StrToUInt64Def(edit1.text, 1);
If (n = 1) And (edit1.text <> '1') Then Begin
memo1.Append('Error, could not handle: ' + Edit1.Text);
exit;
End;
// 1 is not a prime, but we need to plot at least something
If n = 1 Then Begin
memo1.Append('1');
exit;
End;
sqrtn := ceil(sqrt(n)); // Calc upper boarder for shortcut testing ;) -> This speedsup the test by a lot if n is a high prime !
// Calc all prime factors
// The algorithm is a bit like the Sieve of Eratosthenes algorithm, but only for n and not all below n
// Loop Unroll for i = 2
i := 2;
j := 0;
While n Mod i = 0 Do Begin
inc(j);
n := n Div i;
End;
If j <> 0 Then Begin
s := format('%d^%d', [i, j]);
Memo1.Append(s);
End;
// Now test each odd number until n is 1 ;)
i := 3;
While n <> 1 Do Begin
s := '';
j := 0;
While n Mod i = 0 Do Begin
inc(j);
n := n Div i;
End;
If j <> 0 Then Begin
s := format('%d^%d', [i, j]);
Memo1.Append(s);
End;
inc(i, 2);
// Shortcut, what ever "rest" is now left, it is itself a prime ;)
If i > sqrtn Then Begin
i := n;
End;
End;
End;
Procedure TForm1.Edit1KeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState
);
Begin
If key = vk_return Then button1.Click;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Begin
caption := 'Prime factor calc ver. 0.01';
edit1.text := '42';
Button1.Click;
End;
End.