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

- 141.3K All Categories
- 104.6K Programming Languages
- 6.4K Assembler Developer
- 1.9K Basic
- 39.8K C and C++
- 4.3K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.6K Java
- 4.1K Pascal
- 1.3K Perl
- 2K PHP
- 514 Python
- 48 Ruby
- 4.3K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 311 Console programming
- 89 DirectX Game dev
- 1 Minecraft
- 110 Newbie Game Programmers
- 2 Oculus Rift
- 8.9K Applications
- 1.8K Computer Graphics
- 729 Computer Hardware
- 3.4K Database & SQL
- 522 Electronics development
- 1.6K Matlab
- 628 Sound & Music
- 256 XML Development
- 3.3K Classifieds
- 195 Co-operative Projects
- 183 For sale
- 189 FreeLance Software City
- 1.9K Jobs Available
- 600 Jobs Wanted
- 201 Wanted
- 2.9K Microsoft .NET
- 1.7K ASP.NET
- 1.1K .NET General
- 3.3K Miscellaneous
- 4 Join the Team
- 0 User Profiles
- 349 Comments on this site
- 59 Computer Emulators
- 2.1K General programming
- 179 New programming languages
- 610 Off topic board
- 167 Mobile & Wireless
- 41 Android
- 124 Palm Pilot
- 335 Multimedia
- 151 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 19 Cloud Computing
- 53 FreeBSD
- 1.7K LINUX programming
- 367 MS-DOS
- 0 Shell scripting
- 320 Windows CE & Pocket PC
- 4.1K Windows programming
- 889 Software Development
- 406 Algorithms
- 68 Object Orientation
- 87 Project Management
- 90 Quality & Testing
- 237 Security
- 7.5K WEB-Development
- 1.8K Active Server Pages
- 61 AJAX
- 2 Bootstrap Themes
- 55 CGI Development
- 19 ColdFusion
- 222 Flash development
- 1.4K HTML & WEB-Design
- 1.4K Internet Development
- 2.2K JavaScript
- 34 JQuery
- 284 WEB Servers
- 149 WEB-Services / SOAP

outkazt
Posts: **13**Member

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 LLC

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

## Comments

6,349Member:

: 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.

150Member[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.

:

13MemberThen 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,349Member:

: 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.

13MemberI 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,349Member:

: 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.

67Member ✭Read here prime numbers