Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Prime numbers- code not working, Help?


  • Please log in to reply
14 replies to this topic

#1 Hacker4life

Hacker4life

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Location:Serbia
  • Programming Language:C, C++, Delphi/Object Pascal, Pascal, Others
  • Learning:C, Java, C++, (Visual) Basic, Python, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 11 August 2012 - 02:13 AM

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
----------
{Code written by Hacker4life
May the code be with you}
program zad;
var t,a,b,i,j,k:integer;
q:boolean;
begin
read(t);
for j:=1 to t do begin
read(a,<img src='http://cdn.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />;
for i:=a to b do begin
q:=true;
k:=1;
if i>1 then begin
repeat
k:=k+1;
if i mod k=0 then q:=false;
until
(k=i-1)or(q=false);
if i=2 then writeln(2) else
if q then writeln(i); end;
end;
writeln;
end;
end.
some suggestions?
  • 0

#2 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 11 August 2012 - 02:35 AM

I hate those mindfuck tasks. On every single competitions where I was, there was only tasks like that. But this is to most easiest of them. When I get the time I will send you those real mindfuck tasks.
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#3 Hacker4life

Hacker4life

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Location:Serbia
  • Programming Language:C, C++, Delphi/Object Pascal, Pascal, Others
  • Learning:C, Java, C++, (Visual) Basic, Python, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 11 August 2012 - 04:34 AM

ok thx, but do u know whats wrong with my code?
  • 0

#4 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 11 August 2012 - 04:55 AM

I have never worked in Pascal so I can't help you with a code. You will have to wait for someone who does know Pascal. All I can do for you now is to give you some suggestions based on other languages.

Have you runed your code? Where is the bug/error?
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#5 Hacker4life

Hacker4life

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Location:Serbia
  • Programming Language:C, C++, Delphi/Object Pascal, Pascal, Others
  • Learning:C, Java, C++, (Visual) Basic, Python, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 11 August 2012 - 04:59 AM

nope, i think its working fine, spoj will not let me pass even the first test xD
  • 0

#6 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 11 August 2012 - 05:03 AM

spoj?
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#7 Hacker4life

Hacker4life

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Location:Serbia
  • Programming Language:C, C++, Delphi/Object Pascal, Pascal, Others
  • Learning:C, Java, C++, (Visual) Basic, Python, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 11 August 2012 - 06:03 AM

an online programming judge website.
  • 0

#8 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 11 August 2012 - 06:14 AM

some suggestions?


  • Use meaningful variable names. That would make you easier to debug/maintain/extend.
  • Use indentations and good code placement in order to make your code easier to read and to understand, which also make it easier to maintain.
  • Print some "hint" for the user (even if it's just you or your instructor), to make your program a bit more user friendly. Currently your code just waiting for input and spits our numbers. No indication what is it waiting for and what are those number it was printing on the screen.

I modified your code to show you implementation of my recommendation (except for number 1).

var
  t,a,b,i,j,k:integer;
  q:boolean;
begin
  Write('Please enter number of iteration (between 1 and 10): ');
  Read(t);
  for j:=1 to t do
  begin
    Write('Enter the low limit and then the high limit');
    read(a,<img src='http://cdn.codecall.net/public/style_emoticons/<#EMO_DIR#>/cool.png' class='bbc_emoticon' alt='B)' />;
    for i:=a to b do
    begin
      q:=true;
      k:=1;
      if i>1 then
      begin
        repeat
          k:=k+1;
          if i mod k=0 then
            q:=false;
        until (k=i-1)or(q=false);

        if i=2 then
          writeln(2)
        else if q then
          writeln(i);
      end;
    end;
    writeln;
  end;
end.

  • 0

#9 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 11 August 2012 - 06:22 AM

an online programming judge website.


ahaaaa I remember it now :D I am registered there

I will start doing their tasks again :)
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#10 Hacker4life

Hacker4life

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Location:Serbia
  • Programming Language:C, C++, Delphi/Object Pascal, Pascal, Others
  • Learning:C, Java, C++, (Visual) Basic, Python, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 11 August 2012 - 06:58 AM


LuthfiHakim ok thx, but do u know whts wrong with my code?

  • 0

#11 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 11 August 2012 - 07:03 AM


LuthfiHakim ok thx, but do u know whts wrong with my code?


Usually we start by you telling us what's the problem.
  • 0

#12 Hacker4life

Hacker4life

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 77 posts
  • Location:Serbia
  • Programming Language:C, C++, Delphi/Object Pascal, Pascal, Others
  • Learning:C, Java, C++, (Visual) Basic, Python, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 11 August 2012 - 12:08 PM

well, idk, i put my code on an online grader and it doesnt work :S
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download