简体   繁体   English

如何找到整数范围内的向后素数?

[英]How to find backward primes within a range of integers?

I'm trying to solve a backward prime question. 我正在尝试解决一个落后的主要问题。

Following is the question: 以下是问题:

Find all Backwards Read Primes between two positive given numbers (both inclusive), the second one being greater than the first one. 查找两个正数(包括两个正数)之间的所有向后读取素数,第二个大于第一个。 The resulting array or the resulting string will be ordered following the natural order of the prime numbers. 结果数组或结果字符串将按照质数的自然顺序进行排序。

Example

backwardsPrime(2, 100) => [13, 17, 31, 37, 71, 73, 79, 97] 
backwardsPrime(9900, 10000) => [9923, 9931, 9941, 9967]

I tried doing something like this: 我试图做这样的事情:

public function backwardPrime()
{
    $start = 7000;
    $stop = 7100;

    $ans = [];

    while($start <= $stop)
    {
        if($start > 10)
        {
            if($start !== $this->reverse($start))
            {
                if($this->isPrime($start) && $this->isPrime($this->reverse($start)))
                {
                    array_push($ans, $start);
                }
            }
        }
        $start++;
    }
    return $ans;
}

public function reverse($num)
{
    $reverse = 0;
    while($num > 0)
    {
        $reverse = $reverse * 10;
        $reverse = $reverse + $num%10;
        $num = (int)($num/10);
    }
    return $reverse;
}

public function isPrime($num)
{
    if($num == 1 || $num == 2 || $num == 3)
        return true;
    elseif ($num%2 == 0 || $num%3 == 0)
        return false;
    else
    {
        $i=5;
        while($i<=$num/2)
        {
            if($num%$i===0)
            {
                return false;
            }
            $i++;
        }
    }
    return true;
}

I'm able to get the appropriate answer but while doing the same in single function I'm not able to get it: 我能够得到适当的答案,但是在单个函数中执行相同的操作时,我无法得到它:

public function backwardPrimes()
{
    $start = 7000;
    $stop = 7100;
    $ans = [];
    while($start <= $stop)
    {
        $isStartPrime = true;
        $isReversePrime = true;
        if($start > 10)
        {
            $reverse = 0;
            $num = $start;
            while($num > 0)
            {
                $reverse = $reverse * 10;
                $reverse = $reverse + $num%10;
                $num = (int)($num/10);
            }
            if($start !== $reverse)
            {
                if($start%2 != 0 && $start%3 != 0)
                {
                    $i =5;
                    while($i<=$start/2)
                    {
                        if($start%$i === 0)
                        {
                            $isStartPrime = false;
                            break;
                        }
                        $i++;
                    }
                }
                if($reverse%2 != 0 && $reverse%3 != 0)
                {
                    $i =5;
                    while($i<=$reverse/2)
                    {
                        if($reverse%$i === 0)
                        {
                            $isReversePrime = false;
                            break;
                        }
                        $i++;
                    }
                }
                if($isStartPrime && $isReversePrime)
                {
                    array_push($ans, $start);
                }
            }
        }
        $start++;
    }
    return $ans;
}

I don't know where I'm having mistake, guide me. 我不知道我在哪里犯错,引导我。

Thanks. 谢谢。

An emirp ("prime" spelled backwards) is a prime whose (base 10) reversal is also prime, but which is not a palindromic prime. emirp(向后拼写为“素数”)是素数,其(基数10)反转也是素数,但不是回文素数。 in other words Backwards Read Primes are primes that when read backwards in base 10 (from right to left) are a different prime. 换句话说,向后读质数是当以10为底(从右到左)向后读时的质数。 (This rules out primes which are palindromes.) (这排除了回文素。)

try this short solution in which I use two helper function reverse and isPrime : 试试这个简短的解决方案,其中我使用两个辅助函数reverseisPrime

  1. isPrime : Thanks to @Jeff Clayton for his method to test prime numbers, for more information click the link below https://stackoverflow.com/a/24769490/4369087 isPrime :感谢@Jeff克莱顿,他的方法来测试素数,更多信息请点击下面的链接https://stackoverflow.com/a/24769490/4369087
  2. reverse : that use the php function [strrev()][1], this method take a string a reverse it, we'll use this trick to reverse a number by converting it to a string reverse it and converting back to an integer. reverse :使用php函数[strrev()] [1],此方法将字符串取反,我们将使用此技巧来反转数字,方法是将其转换为字符串,然后将其转换回整数。
  3. backwardsPrime : this last function's job is itterating over a range of numbers from $min value to $max value and test if the number if a prime number and it's reverse is a prime number as well and it's not a palindrome number if all of those conditions are true then we addit to the result array. backwardsPrime Prime:该最后一个函数的工作是在从$ min值到$ max值的一系列数字上进行转换,并测试如果质数为正数且取反的数是否也为质数,并且如果所有这些条件都不是回文数是真的,那么我们将其添加到结果数组。

implementation 履行

function isPrime($number) 
{
  return !preg_match('/^1?$|^(11+?)\1+$/x', str_repeat('1', $number));
}

function reverse($n) 
{
    return (int) strrev((string) $n);
}

function backwardsPrime($min, $max)
{
    $result = [];

    foreach(range($min, $max) as $number) {
        $reverse = reverse($number);
        if($reverse !== $number  && isPrime($number) && isPrime($reverse)) {    
           $result[] = $number;
        }
    }

    return $result;
}

echo "<pre>";          
print_r(backwardsPrime(2, 100));
print_r(backwardsPrime(9900, 10000));

output : 输出:

Array
(
    [0] => 13
    [1] => 17
    [2] => 31
    [3] => 37
    [4] => 71
    [5] => 73
    [6] => 79
    [7] => 97
)



   Array
(
    [0] => 9923
    [1] => 9931
    [2] => 9941
    [3] => 9967
)

you can even optimize the backwardsPrime function like this : 您甚至可以像下面那样优化backwardsPrime函数:

function backwardsPrime($min, $max)
{
    $result = [];

    foreach(range($min, $max) as $number) {
        $reverse = reverse($number);
        if($reverse !== $number && !in_array($number, $result) && isPrime($number) && isPrime($reverse)) {  
           $result[] = $number;
        }
    }

    return $result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM