ocehb: (Default)
[personal profile] ocehb
решето эратосфена (числа не больше данного)


#  perl -le '@a = ( 2..$ARGV[0] );
  while (@a) { my $a = shift @a; $a ? print $a : next;
  for (my $i = $a-1; $i < $#a+1; $i += $a) { $a[$i] = 0 } }' 100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
#



Прямое вычисление (количество чисел):


# perl -le '@a = ( $n = 2 );
  n: while ($n++, $#a < $ARGV[0]-1) {
    foreach (@a) { next n if ($n%$_ == 0) }
    push @a, $n 
  }
  END{ print join "\n", @a }' 25
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
#



Ну и сравнение производительности:
Решето:
Real: 0.35s User: 0.32s System: 0.01s Percent: 97%% Cmd: perl -le 102400
Прямое вычисление:
Real: 17.65s User: 17.56s System: 0.00s Percent: 99%% Cmd: perl -le 9805

Profile

ocehb: (Default)
ocehb

January 2021

S M T W T F S
     12
345 6789
10111213141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 9th, 2026 08:54 pm
Powered by Dreamwidth Studios