It looks like you're new here. If you want to get involved, click one of these buttons!

- 140.8K All Categories
- 103.6K Programming Languages
- 6.5K Assembler Developer
- 1.9K Basic
- 39.9K C and C++
- 2.9K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.7K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 543 Python
- 37 Ruby
- 4.4K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 315 Console programming
- 90 DirectX Game dev
- 1 Minecraft
- 112 Newbie Game Programmers
- 2 Oculus Rift
- 9K Applications
- 1.8K Computer Graphics
- 739 Computer Hardware
- 3.4K Database & SQL
- 535 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 257 XML Development
- 3.3K Classifieds
- 199 Co-operative Projects
- 198 For sale
- 190 FreeLance Software City
- 1.9K Jobs Available
- 603 Jobs Wanted
- 208 Wanted
- 2.9K Microsoft .NET
- 1.8K ASP.NET
- 1.1K .NET General
- 3.4K Miscellaneous
- 8 Join the Team
- 354 Comments on this site
- 69 Computer Emulators
- 2.1K General programming
- 187 New programming languages
- 625 Off topic board
- 195 Mobile & Wireless
- 67 Android
- 126 Palm Pilot
- 338 Multimedia
- 154 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 27 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 370 MS-DOS
- 0 Shell scripting
- 321 Windows CE & Pocket PC
- 4.1K Windows programming
- 938 Software Development
- 416 Algorithms
- 68 Object Orientation
- 91 Project Management
- 94 Quality & Testing
- 268 Security
- 7.7K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 4 Bootstrap Themes
- 55 CGI Development
- 28 ColdFusion
- 224 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 36 JQuery
- 300 WEB Servers
- 150 WEB-Services / SOAP

outkazt
Member Posts: **13**

Hey, I am trying to teach myself how to program in delphi/pascal.

I want to design two different apps first, determine whenther an input number is prime or not and second, generate me the first 20-30 prime numbers.

Any tips here?

I want to design two different apps first, determine whenther an input number is prime or not and second, generate me the first 20-30 prime numbers.

Any tips here?

Tagged:

Terms of use / Privacy statement / Publisher: Lars Hagelin

Programmers Heaven articles / Programmers Heaven files / Programmers Heaven uploaded content / Programmers Heaven C Sharp ebook / Operated by CommunityHeaven

© 1997-2015 Programmersheaven.com - All rights reserved.

## Comments

6,349:

: I want to design two different apps first, determine whenther an input number is prime or not and second, generate me the first 20-30 prime numbers.

:

: Any tips here?

:

1st: Create a form with a TEdit and a TButton. In the TButton.OnClick() convert the TEdit.Text property to an integer. Then use a for-loop to loop through all the numbers from 2 to the entered number-1, and check if the "mod" operation is 0. If one of those operations gives a 0, it is not a prime. This is a very slow method of coding this program, but it works.

2nd: Use a TMemo instead of a TEdit. Add a repeat-until loop to the TButton, which loops until the TEdit.Lines.Count is within 20-30. Before the repeat-until loop, set the integer to 3. Then after the prime check, add 1 to this integer. Each time the check is passed (integer = prime) use TMemo.Lines.Add() to add it to the list. This will find the specified number of primes, except the prime 2. This code can be optimalized by changing the check loop.

150[code]

function IsPrime (N : integer) : Boolean;

var

i, SqrtOfN : integer;

begin

// if N is smaller than 2, it cannot be a prime number

if N < 2 then

begin

Result := False;

exit

end;

// If it is not smaller than 2...

// First of all, assume that the given number N is a prime number.

Result := True;

// Then, check from 2 until to the square root of N if it is a divisor

// of N or not. If you can find such a divisor, it means that N

// is not prime, and you can stop at that point.

SqrtOfN := round(sqrt(N));

for i := 2 to SqrtOfN do

if (N mod i) = 0 then

begin

Result := False;

break

end

end;

[/code]

And the following procedure adds all of the existing primes smaller than

or equal to the given integer to the list.

[code]

procedure AddAllPrimesTillN (N : integer; Memo : TMemo);

var

i : integer;

begin

Memo.Lines.Clear;

for i := 2 to N do

if IsPrime(i) then

Memo.Lines.Add(IntToStr(i))

end;

[/code]

: : Hey, I am trying to teach myself how to program in delphi/pascal.

: :

: : I want to design two different apps first, determine whenther an input number is prime or not and second, generate me the first 20-30 prime numbers.

: :

: : Any tips here?

: :

: 1st: Create a form with a TEdit and a TButton. In the TButton.OnClick() convert the TEdit.Text property to an integer. Then use a for-loop to loop through all the numbers from 2 to the entered number-1, and check if the "mod" operation is 0. If one of those operations gives a 0, it is not a prime. This is a very slow method of coding this program, but it works.

: 2nd: Use a TMemo instead of a TEdit. Add a repeat-until loop to the TButton, which loops until the TEdit.Lines.Count is within 20-30. Before the repeat-until loop, set the integer to 3. Then after the prime check, add 1 to this integer. Each time the check is passed (integer = prime) use TMemo.Lines.Add() to add it to the list. This will find the specified number of primes, except the prime 2. This code can be optimalized by changing the check loop.

:

13Then another program that can generate the first 20 prime numbers?

I am using software delphi4, quite old I know!

: The following function fins whether a number is prime or not.

:

: [code]

: function IsPrime (N : integer) : Boolean;

: var

: i, SqrtOfN : integer;

: begin

: // if N is smaller than 2, it cannot be a prime number

:

: if N < 2 then

: begin

: Result := False;

: exit

: end;

:

: // If it is not smaller than 2...

:

: // First of all, assume that the given number N is a prime number.

:

: Result := True;

:

: // Then, check from 2 until to the square root of N if it is a divisor

: // of N or not. If you can find such a divisor, it means that N

: // is not prime, and you can stop at that point.

:

: SqrtOfN := round(sqrt(N));

:

: for i := 2 to SqrtOfN do

: if (N mod i) = 0 then

: begin

: Result := False;

: break

: end

: end;

: [/code]

:

: And the following procedure adds all of the existing primes smaller than

: or equal to the given integer to the list.

:

: [code]

: procedure AddAllPrimesTillN (N : integer; Memo : TMemo);

: var

: i : integer;

: begin

: Memo.Lines.Clear;

:

: for i := 2 to N do

: if IsPrime(i) then

: Memo.Lines.Add(IntToStr(i))

: end;

: [/code]

:

:

:

: : : Hey, I am trying to teach myself how to program in delphi/pascal.

: : :

: : : I want to design two different apps first, determine whenther an input number is prime or not and second, generate me the first 20-30 prime numbers.

: : :

: : : Any tips here?

: : :

: : 1st: Create a form with a TEdit and a TButton. In the TButton.OnClick() convert the TEdit.Text property to an integer. Then use a for-loop to loop through all the numbers from 2 to the entered number-1, and check if the "mod" operation is 0. If one of those operations gives a 0, it is not a prime. This is a very slow method of coding this program, but it works.

: : 2nd: Use a TMemo instead of a TEdit. Add a repeat-until loop to the TButton, which loops until the TEdit.Lines.Count is within 20-30. Before the repeat-until loop, set the integer to 3. Then after the prime check, add 1 to this integer. Each time the check is passed (integer = prime) use TMemo.Lines.Add() to add it to the list. This will find the specified number of primes, except the prime 2. This code can be optimalized by changing the check loop.

: :

:

:

6,349:

: Then another program that can generate the first 20 prime numbers?

:

: I am using software delphi4, quite old I know!

:

:

: : The following function fins whether a number is prime or not.

: :

: : [code]

: : function IsPrime (N : integer) : Boolean;

: : var

: : i, SqrtOfN : integer;

: : begin

: : // if N is smaller than 2, it cannot be a prime number

: :

: : if N < 2 then

: : begin

: : Result := False;

: : exit

: : end;

: :

: : // If it is not smaller than 2...

: :

: : // First of all, assume that the given number N is a prime number.

: :

: : Result := True;

: :

: : // Then, check from 2 until to the square root of N if it is a divisor

: : // of N or not. If you can find such a divisor, it means that N

: : // is not prime, and you can stop at that point.

: :

: : SqrtOfN := round(sqrt(N));

: :

: : for i := 2 to SqrtOfN do

: : if (N mod i) = 0 then

: : begin

: : Result := False;

: : break

: : end

: : end;

: : [/code]

: :

: : And the following procedure adds all of the existing primes smaller than

: : or equal to the given integer to the list.

: :

: : [code]

: : procedure AddAllPrimesTillN (N : integer; Memo : TMemo);

: : var

: : i : integer;

: : begin

: : Memo.Lines.Clear;

: :

: : for i := 2 to N do

: : if IsPrime(i) then

: : Memo.Lines.Add(IntToStr(i))

: : end;

: : [/code]

: :

: :

: :

: : : : Hey, I am trying to teach myself how to program in delphi/pascal.

: : : :

: : : : I want to design two different apps first, determine whenther an input number is prime or not and second, generate me the first 20-30 prime numbers.

: : : :

: : : : Any tips here?

: : : :

: : : 1st: Create a form with a TEdit and a TButton. In the TButton.OnClick() convert the TEdit.Text property to an integer. Then use a for-loop to loop through all the numbers from 2 to the entered number-1, and check if the "mod" operation is 0. If one of those operations gives a 0, it is not a prime. This is a very slow method of coding this program, but it works.

: : : 2nd: Use a TMemo instead of a TEdit. Add a repeat-until loop to the TButton, which loops until the TEdit.Lines.Count is within 20-30. Before the repeat-until loop, set the integer to 3. Then after the prime check, add 1 to this integer. Each time the check is passed (integer = prime) use TMemo.Lines.Add() to add it to the list. This will find the specified number of primes, except the prime 2. This code can be optimalized by changing the check loop.

: : :

: :

: :

:

:

You will probably need a TEdit and a TButton. In the TButton.OnClick() event, you may first want to check if the entered text is indeed a number. The shortest way to code this is to use a try-except block and a conversion:

[code]

try

Value := StrToInt(Edit1.Text);

except

ShowMessage('Must enter an integer');

Exit;

end;

[/code]

Then you can call one of the functions given in previous posts to check if the number was a prime. The result can be quickly shown using the ShowMessage() procedure.

13I am running software version delphi 4.

I need the program, when run, to bring up the black app window and request an interger number, and on input, calculate if it is prime or not.

Then an extension of the same app that generates the first 20 primes.

Is this possible?

6,349:

: I am running software version delphi 4.

:

: I need the program, when run, to bring up the black app window and request an interger number, and on input, calculate if it is prime or not.

:

: Then an extension of the same app that generates the first 20 primes.

:

: Is this possible?

:

Here is a simple tested console project which should work:

[code]

program testconsole;

{$APPTYPE CONSOLE}

uses

SysUtils;

var

i, Value: integer;

IsPrime: boolean;

begin

write('Input integer: '); readln(Value);

{ Actual prime check }

IsPrime := true; // Assume a prime

for i := Value-1 downto 2 do

if Value mod i = 0 then // if dividable by another numer

IsPrime := false; // then it is not a prime

{ Show result }

if IsPrime then

writeln(Value,' is prime')

else

writeln(Value,' is not prime');

readln;

end.

[/code]

The following code will generate the first n primes:

[code]

program testconsole;

{$APPTYPE CONSOLE}

uses

SysUtils;

const

n = 20;

var

i, j, Value: integer;

IsPrime: boolean;

Primes: array[1..n] of integer;

begin

Primes[1] := 2; // Add 2 as 1st prime

j := 2; // Start by finding 2nd prime

Value := 3; // Start by checking the value 3

repeat

{ Actual prime check: Same as previous program }

IsPrime := true;

for i := Value-1 downto 2 do

if Value mod i = 0 then

IsPrime := false;

if IsPrime then

begin // Add found prime to list

Primes[j] := Value;

inc(j);

end;

inc(Value, 2); // Increase Value by 2 to skip even numbers

until j = n+1; // Keep repeating until j > n

{ Show output to user }

for i := 1 to n do

write(Primes[i], ' ');

readln;

end.

[/code]

This code is certainly not speed-optimalized, but the previously posted functions can be used for the check itself.

67✭Read here prime numbers