询问大量素数时跳过一些素数的计算
Skipping calculation of somes primes when asking lot of primes
我刚开始学习一些 ada 代码,并且会创建我自己的素数计算器。
为了进行处理,我使用了一种最著名的方法,即:
"each primes is a result of 6 * x -+ 1 "
所以这是我的代码:
with Ada.Text_IO, Ada.Integer_Text_IO ;
use Ada.Text_IO, Ada.Integer_Text_IO ;
procedure main is
count_prime : Integer := 0 ;
counter : Integer := 1 ;
wanted : Integer ;
iteration : Integer := 0 ;
testing : Integer := 0 ;
is_prime : Boolean ;
answer : Character ;
begin
loop
Put("Prime calculator") ;
New_line(2) ;
Put("Put 'p' to process") ;
New_Line(1);
Put("Put 'q' to quit") ;
New_Line(2) ;
Put(">> ") ;
Get(answer) ;
if answer = 'p' then
Put("Enter wanted primes :");
Get(wanted) ;
Skip_line ;
if wanted > 0 then
Put("2");
New_Line(1);
if wanted > 1 then
Put("3");
New_Line(1);
end if ;
if wanted > 2 then
count_prime := 2;
loop
if counter = 1 then
counter := 0 ;
iteration := iteration + 1 ;
testing := ( 6 * iteration ) - 1 ;
else
counter := 1 ;
testing := ( 6 * iteration ) + 1 ;
end if ;
is_prime := True ;
for i in 2..(testing-1) loop
if (testing rem i = 0) then
is_prime := False ;
end if ;
end loop;
if is_prime = True then
Put(testing);
New_Line(1);
count_prime := count_prime + 1 ;
end if ;
exit when count_prime = wanted;
end loop ;
end if;
Put("Ended") ;
else
Put("It's can't be a negative number");
end if ;
end if ;
New_Line(3);
exit when answer = 'q' ;
end loop ;
end main ;
我真的知道这是一个基本的,我的意思是,非常基本的程序。但我只想解决我提出的问题:
与 'p' 和 2 :
2
3
'p' 和“7”
2
3
5
7
11
13
17
与 'p' 和 1200
2
3
19
23
29
31
37
41
....
3 到 19 之间的所有质数都去哪儿了?
if wanted > 2 then
count_prime := 2;
-- 您可能想在此处重置 iteration
...
iteration := 0;
loop
if counter = 1 then
你保持运行循环计算,但不要重置它的初始状态。执行计算的 loop
继续使用 iteration
、counter
和之前 运行.
中的一些其他变量的值
要么将循环分解成一个单独的过程,要么至少用 declare
块包围它,例如:
declare
count_prime : Integer := 2;
counter : Integer := 1;
iteration : Integer := 0;
testing : Integer := 0;
is_prime : Boolean;
begin
loop
…
end loop;
end;
但是,我强烈建议分解成一个单独的 procedure
。
我刚开始学习一些 ada 代码,并且会创建我自己的素数计算器。 为了进行处理,我使用了一种最著名的方法,即: "each primes is a result of 6 * x -+ 1 "
所以这是我的代码:
with Ada.Text_IO, Ada.Integer_Text_IO ;
use Ada.Text_IO, Ada.Integer_Text_IO ;
procedure main is
count_prime : Integer := 0 ;
counter : Integer := 1 ;
wanted : Integer ;
iteration : Integer := 0 ;
testing : Integer := 0 ;
is_prime : Boolean ;
answer : Character ;
begin
loop
Put("Prime calculator") ;
New_line(2) ;
Put("Put 'p' to process") ;
New_Line(1);
Put("Put 'q' to quit") ;
New_Line(2) ;
Put(">> ") ;
Get(answer) ;
if answer = 'p' then
Put("Enter wanted primes :");
Get(wanted) ;
Skip_line ;
if wanted > 0 then
Put("2");
New_Line(1);
if wanted > 1 then
Put("3");
New_Line(1);
end if ;
if wanted > 2 then
count_prime := 2;
loop
if counter = 1 then
counter := 0 ;
iteration := iteration + 1 ;
testing := ( 6 * iteration ) - 1 ;
else
counter := 1 ;
testing := ( 6 * iteration ) + 1 ;
end if ;
is_prime := True ;
for i in 2..(testing-1) loop
if (testing rem i = 0) then
is_prime := False ;
end if ;
end loop;
if is_prime = True then
Put(testing);
New_Line(1);
count_prime := count_prime + 1 ;
end if ;
exit when count_prime = wanted;
end loop ;
end if;
Put("Ended") ;
else
Put("It's can't be a negative number");
end if ;
end if ;
New_Line(3);
exit when answer = 'q' ;
end loop ;
end main ;
我真的知道这是一个基本的,我的意思是,非常基本的程序。但我只想解决我提出的问题:
与 'p' 和 2 :
2
3
'p' 和“7”
2
3
5
7
11
13
17
与 'p' 和 1200
2
3
19
23
29
31
37
41
....
3 到 19 之间的所有质数都去哪儿了?
if wanted > 2 then
count_prime := 2;
-- 您可能想在此处重置 iteration
...
iteration := 0;
loop
if counter = 1 then
你保持运行循环计算,但不要重置它的初始状态。执行计算的 loop
继续使用 iteration
、counter
和之前 运行.
要么将循环分解成一个单独的过程,要么至少用 declare
块包围它,例如:
declare
count_prime : Integer := 2;
counter : Integer := 1;
iteration : Integer := 0;
testing : Integer := 0;
is_prime : Boolean;
begin
loop
…
end loop;
end;
但是,我强烈建议分解成一个单独的 procedure
。