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

- 140.7K All Categories
- 104.4K Programming Languages
- 6.4K Assembler Developer
- 1.8K Basic
- 39.7K C and C++
- 4.2K C#
- 7.9K Delphi and Kylix
- 4 Haskell
- 9.6K Java
- 4.1K Pascal
- 1.3K Perl
- 1.9K PHP
- 505 Python
- 48 Ruby
- 4.3K VB.NET
- 1.6K VBA
- 20.8K Visual Basic
- 2.6K Game programming
- 309 Console programming
- 88 DirectX Game dev
- 1 Minecraft
- 108 Newbie Game Programmers
- 2 Oculus Rift
- 8.9K Applications
- 1.8K Computer Graphics
- 726 Computer Hardware
- 3.4K Database & SQL
- 520 Electronics development
- 1.6K Matlab
- 627 Sound & Music
- 254 XML Development
- 3.3K Classifieds
- 189 Co-operative Projects
- 178 For sale
- 189 FreeLance Software City
- 1.9K Jobs Available
- 599 Jobs Wanted
- 201 Wanted
- 2.9K Microsoft .NET
- 1.7K ASP.NET
- 1.1K .NET General
- 3K Miscellaneous
- 3 Join the Team
- 2 User Profiles
- 353 Comments on this site
- 59 Computer Emulators
- 1.8K General programming
- 159 New programming languages
- 597 Off topic board
- 163 Mobile & Wireless
- 37 Android
- 124 Palm Pilot
- 335 Multimedia
- 151 Demo programming
- 184 MP3 programming
- 0 Bash scripts
- 16 Cloud Computing
- 52 FreeBSD
- 1.7K LINUX programming
- 366 MS-DOS
- 0 Shell scripting
- 319 Windows CE & Pocket PC
- 4.1K Windows programming
- 884 Software Development
- 403 Algorithms
- 67 Object Orientation
- 85 Project Management
- 88 Quality & Testing
- 233 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
- 33 JQuery
- 285 WEB Servers
- 112 WEB-Services / SOAP

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

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:

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 · Share on Facebook150Member[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 · Share on Facebook13MemberThen 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 · Share on Facebook6,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 · Share on Facebook13MemberI 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 · Share on Facebook6,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 · Share on Facebook67Member ✭Read here prime numbers

- Spam

0 · Vote Down Vote Up · Share on Facebook