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

- All Categories 139.8K
- Programming Languages 104K
- Assembler Developer 6.2K
- Basic 1.8K
- C and C++ 39.7K
- C# 4.2K
- Delphi and Kylix 7.9K
- Haskell 3
- Java 9.5K
- Pascal 4.1K
- Perl 1.3K
- PHP 1.9K
- Python 500
- Ruby 47
- VB.NET 4.3K
- VBA 1.6K
- Visual Basic 20.8K
- Game programming 2.6K
- Console programming 308
- DirectX Game dev 87
- Minecraft 1
- Newbie Game Programmers 107
- Oculus Rift 2
- Applications 8.8K
- Computer Graphics 1.8K
- Computer Hardware 721
- Database & SQL 3.4K
- Electronics development 517
- Matlab 1.6K
- Sound & Music 625
- XML Development 253
- Classifieds 3.2K
- Co-operative Projects 184
- For sale 161
- FreeLance Software City 188
- Jobs Available 1.9K
- Jobs Wanted 597
- Wanted 192
- Microsoft .NET 2.8K
- ASP.NET 1.7K
- .NET General 1.1K
- Miscellaneous 2.8K
- Join the Team 2
- User Profiles 1
- Comments on this site 351
- Computer Emulators 54
- General programming 1.7K
- New programming languages 114
- Off topic board 591
- Mobile & Wireless 146
- Android 20
- Palm Pilot 124
- Multimedia 334
- Demo programming 151
- MP3 programming 183
- Bash scripts 0
- Cloud Computing 10
- FreeBSD 52
- LINUX programming 1.7K
- MS-DOS 361
- Shell scripting 0
- Windows CE & Pocket PC 317
- Windows programming 4.1K
- Software Development 870
- Algorithms 400
- Object Orientation 67
- Project Management 80
- Quality & Testing 87
- Security 230
- WEB-Development 7.3K
- Active Server Pages 1.8K
- AJAX 61
- Bootstrap Themes 1
- CGI Development 55
- ColdFusion 19
- Flash development 221
- HTML & WEB-Design 1.4K
- Internet Development 1.3K
- JavaScript 2.2K
- JQuery 33
- WEB Servers 275
- WEB-Services / SOAP 100

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.

Tweets by @pheaven
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

outkazt
Posts: **13**Member

About & Contact / 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-2013 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.

- Spam

0 · Vote Down Vote Up ·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.

:

- Spam

0 · Vote Down Vote Up ·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.

: :

:

:

- Spam

0 · Vote Down Vote Up ·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.

- Spam

0 · Vote Down Vote Up ·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?

- Spam

0 · Vote Down Vote Up ·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.

- Spam

0 · Vote Down Vote Up ·67Member ✭Read here prime numbers

- Spam

0 · Vote Down Vote Up ·