Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

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

Prime numbers help!

outkaztoutkazt Posts: 13Member
edited October 2013 in Delphi and Kylix
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?
Tagged:

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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.
  • _yilmaz_yilmaz Posts: 150Member
    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.
    :

  • outkaztoutkazt Posts: 13Member
    What would I code if I wanted the application to ask for input, ie a number, then for the program to figure out whether the input number is prime or not?

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

  • zibadianzibadian Posts: 6,349Member
    : What would I code if I wanted the application to ask for input, ie a number, then for the program to figure out whether the input number is prime or not?
    :
    : 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.
  • outkaztoutkazt Posts: 13Member
    The code didn't work :|

    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?
  • zibadianzibadian Posts: 6,349Member
    : The code didn't work :|
    :
    : 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.
Sign In or Register to comment.