Ada 询问大量素数时跳过某些素数的计算

Ada 询问大量素数时跳过某些素数的计算,ada,Ada,我刚开始学习一些ada代码,并将创建我自己的素数计算器。 为了繁衍后代,我使用了一种最为人所知的方法,即: “每个素数是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 :

我刚开始学习一些ada代码,并将创建我自己的素数计算器。 为了繁衍后代,我使用了一种最为人所知的方法,即: “每个素数是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 := 0;
           loop
              if counter = 1 then

在一个循环中继续运行计算,但不重置其初始状态。执行计算的
循环继续使用
迭代
计数器
的值和上一次运行中的一些其他变量

将循环分解为一个单独的过程,或者至少用
declare
块将其包围,例如:

declare
  count_prime : Integer := 2;
  counter : Integer := 1;
  iteration : Integer := 0;
  testing : Integer := 0;
  is_prime : Boolean;
begin
  loop
    …
    end loop;
  end;

然而,我强烈建议将其分解为一个单独的
过程

我对变量进行了重置,但当我输入4时,我的程序将无限运行