Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/perl
- # Is there a prime wich after the 1000th prime is exactley bigger by
- # 8000 / 9000 / 10000?
- # 1000th_prime - prime == 8000 / 9000 / 10000;
- use strict;
- use warnings;
- sub prime($) {
- my $number = shift;
- return 1 if ($number == 2);
- my $d = 2;
- my $sqrt = sqrt($number);
- while (1) {
- return 0 if ($number % $d == 0);
- if ($d < $sqrt) {
- $d++;
- } else {
- return 1;
- }
- }
- }
- my @res;
- for (my $diff = 8000; $diff <= 10000; $diff += 1000) {
- # find a prime, call it 1st
- my $first = 1;
- OUTER: while (1) {
- $first += 2;
- next unless prime($first);
- # find the 1000th prime ($next) to the 1st prime
- my $last = $first;
- my $next = 0;
- while (1) {
- $last += 2;
- next unless prime($last);
- $next++;
- # check the 1000th prime
- if ($next == 1000) {
- printf("%-40s", "1000th prim after $first = $last");
- my $difference = $last - $first;
- # if it fit, search is over :)
- if ($difference == $diff) {
- print "I FOUND IT! " x 3 . "\n";
- push(@res, "$diff == $first-$last ($next.)\n");
- last OUTER;
- }
- # if it's too big, search is over
- elsif ($difference > $diff) {
- print "difference is too big $difference\n";
- last OUTER;
- }
- # if it's too small, get a bigger 1st prime!
- else {
- print "difference is too small $difference\n";
- last
- }
- }
- }
- }
- }
- print "\n" x 2 . join('',@res);
Advertisement
Add Comment
Please, Sign In to add comment