fork(5) download
  1. program fermath_test_for_prime_numbers;
  2. function bigmultiplication(x,y,n:longint):longint;
  3. var
  4. doi:longint;
  5. begin
  6. doi:=0;
  7. while(y>0) do
  8. begin
  9. if(y mod 2 =1) then
  10. begin
  11. doi:=(doi+x)mod n;
  12. end;
  13.  
  14. x:=(2*x)mod n;
  15. y:=y div 2;
  16. end;
  17. bigmultiplication:=doi;
  18. end;
  19.  
  20.  
  21.  
  22.  
  23. function exponents_mod_n(x,y,n:longint):longint;
  24. var
  25. doi,doing:longint;
  26. begin
  27. doing:=x;
  28. doi:=1;
  29. while(y>0) do
  30. begin
  31. if(y mod 2 =1) then
  32. begin
  33. doi:=bigmultiplication(doi,doing,n);
  34. end;
  35. doing:=bigmultiplication(doing,doing,n);
  36. y:=y div 2;
  37. end;
  38. exponents_mod_n:=doi;
  39. end;
  40. var
  41. a,p,x,n:longint;
  42. i,flag:integer;
  43. begin
  44. Randomize;
  45.  
  46. readln(p);
  47. flag:=0;
  48. if p mod 2 = 0 then
  49. begin
  50. writeln('the number is composite');
  51.  
  52. end;
  53. for i:=1 to 7 do
  54. begin
  55.  
  56.  
  57. x:=p-1;
  58. a:=random(x)+1 mod x;
  59. n:=exponents_mod_n(a,x,p);
  60. if n<>1 then
  61. begin
  62. writeln('the number is composite');
  63. flag:=1;
  64. break;
  65. end;
  66.  
  67. end;
  68. if flag = 0 then
  69. writeln('this is prime number');
  70. end.
  71.  
stdin
239
compilation info
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
Linking prog
70 lines compiled, 0.0 sec
stdout
this is prime number