Check whether N is a Dihedral Prime Number or not in Python

PythonServer Side ProgrammingProgramming

<p>Suppose we have a number n. We have to check whether n is dihedral prime or not. A number is said to be dihedral prime when that number is itself prime and also shown same number or any other prime number using 7-segment display regardless the orientation of the display (normal or up-side down).</p><p>So, if the input is like n = 1181, then the output will be True</p><p><img src="" class="fr-fic fr-dib" width="328" height="148"></p><p>second one is up-side down format of the first one and both are prime.</p><p>To solve this, we will follow these steps &minus;</p><ul class="list"><li>Define a function up_side_down() . This will take n</li><li>temp := n, total := 0</li><li>while temp &gt; 0, do<ul class="list"><li>d := temp mod 10</li><li>if d is same as 2, then d := 5</li><li>otherwise when d is same as 5, then d := 2</li><li>total := total * 10 + d</li><li>temp := quotient of (temp / 10)</li></ul></li><li>return total</li><li>From the main method do the following:</li><li>if n is not prime or up_side_down(n) is not prime or reverse of n is not prime or reverse of up_side_down(n) is not prime, then<ul class="list"><li>return False</li></ul></li><li>temp := n</li><li>while temp &gt; 0, do<ul class="list"><li>rem := temp mod 10</li><li>if rem is any of these [3, 4, 6, 7, 9], then<ul class="list"><li>return False</li></ul></li><li>temp := quotient of (temp / 10)</li></ul></li><li>return True</li></ul><p>Let us see the following implementation to get better understanding &minus;</p><h2>Example Code</h2><p><a class="demo" href="" rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">prime = (int(1e5)+5)*[True] def reverse(n): &nbsp; &nbsp;return int(str(n)[::-1]) &nbsp; def up_side_down(n): &nbsp; &nbsp;temp = n &nbsp; &nbsp;total = 0 &nbsp; &nbsp;while temp&gt;0: &nbsp; &nbsp; &nbsp; d = temp % 10 &nbsp; &nbsp; &nbsp; if d == 2: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d = 5 &nbsp; &nbsp; &nbsp; elif d == 5: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d = 2 &nbsp; &nbsp; &nbsp; total = total * 10 + d &nbsp; &nbsp; &nbsp; temp//= 10 &nbsp; &nbsp; &nbsp;return total &nbsp; def get_all_prime(): &nbsp; &nbsp;prime[0] = prime[1] = False &nbsp; &nbsp; &nbsp;for i in range(2, int(1e5)+1): &nbsp; &nbsp; &nbsp; j = 2 &nbsp; &nbsp; &nbsp; while i * j&lt;= int(1e5): &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prime[i * j] = False &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j+= 1 &nbsp; def solve(n): &nbsp; &nbsp;get_all_prime() &nbsp; &nbsp;if not prime[n] or not prime[up_side_down(n)] or not prime[reverse(n)] or not prime[reverse(up_side_down(n))]: &nbsp; &nbsp; &nbsp; return False &nbsp; &nbsp; &nbsp;temp = n &nbsp; &nbsp; &nbsp;while temp&gt;0: &nbsp; &nbsp; &nbsp; rem = temp % 10; &nbsp; &nbsp; &nbsp; if rem in [3, 4, 6, 7, 9]: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return False &nbsp; &nbsp; &nbsp; temp //= 10 &nbsp; &nbsp; &nbsp;return True n = 1181 print(solve(n)) </pre><h2>Input</h2><pre class="result notranslate">23, 3</pre><h2>Output</h2><pre class="result notranslate">True </pre>
Updated on 16-Jan-2021 04:38:01