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 : 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 之间的所有素数都去哪儿了?

最佳答案

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

要么将循环分解为一个单独的过程,要么至少用 declare block 包围它,例如:

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

但是,我强烈建议分解为单独的过程

关于ada - 当询问很多素数时跳过一些素数的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58268407/

相关文章:

ada - 如何在 Ada 的数学运算中使用不同的固定点类型?

linux - Ada GNAT.Serial_Communications 在 Linux 上的行为

null - 从 Ada 中的地址 0x0 读取

concurrency - Ada 并发问题

C++ vs. D、Ada 和 Eiffel(带有模板的可怕错误消息)

ada - Glade-3 for Ada

string - 使用规则约束或子类型 Ada 中的 Unbounded_String?

algorithm - 存储错误-第一次深度搜索算法

Ada:包不允许有正文

ada - 编译器不允许我获取字符串