logo

Prime truncabil la dreapta

Un prim trunchiabil la dreapta este un prim care rămâne prim atunci când ultima cifră („dreapta”) este eliminată succesiv. De exemplu, 239 este primul trunchiabil la dreapta, deoarece 239 23 și 2 sunt toate prime. Există 83 de numere prime trunchiabile la dreapta.
Sarcina este de a verifica dacă numărul dat (N > 0) este prim trunchiabil la dreapta sau nu. 
Exemple:  
 

  Input:   239   Output:   Yes   Input:   101   Output:   No 101 is not right-truncatable prime because numbers formed are 101 10 and 1. Here 101 is prime but 10 and 1 are not prime.


 


Ideea este de a genera toate numerele prime mai mici sau egale cu numărul dat N folosind Sita lui Eratosthenes . Odată ce am generat toate astfel de numere prime, atunci verificăm dacă numărul rămâne prim atunci când ultima cifră („dreapta”) este eliminată succesiv.
 



C++
//C++ Program to check  // whether a given number // is right-truncatable  // prime or not. #include   using namespace std; // Generate all prime numbers less than n. bool sieveOfEratosthenes(int n bool isPrime[]) {  // Initialize all entries  // of boolean array as   // true. A value in  // isPrime[i] will finally  // be false if i is Not a   // prime else true  // bool isPrime[n+1];  isPrime[0] = isPrime[1] = false;  for( int i = 2; i <= n; i++)  isPrime[i] = true;  for (int p = 2; p * p<=n; p++)  {  // If isPrime[p] is not changed then it is  // a prime  if (isPrime[p] == true)  {  // Update all multiples of p  for (int i = p * 2; i <= n; i += p)  isPrime[i] = false;  }  } } // Returns true if n is right-truncatable // else false bool rightTruPrime(int n) {  // Generating primes using Sieve  bool isPrime[n+1];  sieveOfEratosthenes(n isPrime);  // Checking whether the number remains  // prime when the last ('right')   // digit is successively removed  while (n)  {  if (isPrime[n])   n = n / 10;  else  return false;  }  return true;  } // Driver program int main() {  int n = 59399;  if (rightTruPrime(n))  cout << 'Yes' << endl;  else  cout << 'No' << endl;  return 0; } 
Java
// Java code to check  // right-truncatable  // prime or not. import java.io.*; class GFG {    // Generate all prime  // numbers less than n.  static void sieveOfEratosthenes  (int n boolean isPrime[])   {    // Initialize all entries of  // boolean array as true. A  // value in isPrime[i] will  // finally be false if i is  // Not a prime else true  // bool isPrime[n+1];  isPrime[0] = isPrime[1] = false;  for (int i = 2; i <= n; i++)  isPrime[i] = true;    for (int p=2; p*p<=n; p++)  {  // If isPrime[p] is not   // changed then it   // is a prime  if (isPrime[p] == true)  {  // Update all multiples of p  for (int i = p * 2; i <= n; i += p)  isPrime[i] = false;  }  }  }    // Returns true if n is  // right-truncatable   // else false  static boolean rightTruPrime(int n)  {    // Generating primes using Sieve  boolean isPrime[] = new boolean[n+1];  sieveOfEratosthenes(n isPrime);    // Checking whether the number  // remains prime when the last (right)  // digit is successively removed  while (n != 0)  {    if (isPrime[n])  n = n / 10;   else  return false;  }  return true;  }    // Driver program  public static void main(String args[])  {  int n = 59399;    if (rightTruPrime(n))  System.out.println('Yes');  else  System.out.println('No');  } } /* This code is contributed by Nikita Tiwari.*/ 
Python3
# Python3 Program to check # whether a given number # is right-truncatable  # prime or not. # Generate all prime numbers less than n. def sieveOfEratosthenes(nisPrime) : # Initialize all entries  # of boolean array as  # true. A value in isPrime[i]  # will finally be false if # i is Not a prime else true # bool isPrime[n+1]; isPrime[0] = isPrime[1] = False for i in range(2 n+1) : isPrime[i] = True p = 2 while(p * p <= n) : # If isPrime[p] is not changed then it is # a prime if (isPrime[p] == True) : # Update all multiples of p i = p * 2 while(i <= n) : isPrime[i] = False i = i + p p = p + 1 # Returns true if n is right-truncatable else false def rightTruPrime(n) : # Generating primes using Sieve isPrime=[None] * (n+1) sieveOfEratosthenes(n isPrime) # Checking whether the  # number remains prime # when the last ('right') # digit is successively # removed while (n != 0) : if (isPrime[n]) : n = n // 10 else : return False return True # Driven program n = 59399 if (rightTruPrime(n)) : print('Yes') else : print('No') # This code is contributed by Nikita Tiwari. 
C#
// C# code to check right- // truncatable prime or not using System; class GFG {  // Generate all prime  // numbers less than n.  static void sieveOfEratosthenes(int n bool[] isPrime)  {  // Initialize all entries of  // boolean array as true. A  // value in isPrime[i] will  // finally be false if i is  // Not a prime else true  // bool isPrime[n+1];  isPrime[0] = isPrime[1] = false;  for (int i = 2; i <= n; i++)  isPrime[i] = true;  for (int p = 2; p * p <= n; p++) {  // If isPrime[p] is not  // changed then it  // is a prime  if (isPrime[p] == true) {  // Update all multiples of p  for (int i = p * 2; i <= n; i += p)  isPrime[i] = false;  }  }  }  // Returns true if n is right-  // truncatable else false  static bool rightTruPrime(int n)  {  // Generating primes using Sieve  bool[] isPrime = new bool[n + 1];  sieveOfEratosthenes(n isPrime);  // Checking whether the number  // remains prime when last (right)  // digit is successively removed  while (n != 0) {  if (isPrime[n])  n = n / 10;  else  return false;  }  return true;  }  // Driven program  public static void Main()  {  int n = 59399;  if (rightTruPrime(n))  Console.WriteLine('Yes');  else  Console.WriteLine('No');  } } // This code is contributed by Anant Agarwal 
PHP
 // Program to check whether a given number  // is right-truncatable prime or not.  // Generate all prime numbers less than n.  function sieveOfEratosthenes($n &$isPrime) { // Initialize all entries of boolean  // array as true. A value in isPrime[i]  // will finally be false if i is Not a  // prime else true bool isPrime[n+1];  $isPrime[0] = $isPrime[1] = false; for ($p = 2; $p * $p <= $n; $p++) { // If isPrime[p] is not changed  // then it is a prime  if ($isPrime[$p] == true) { // Update all multiples of p  for ($i = $p * 2; $i <= $n; $i += $p) $isPrime[$i] = false; } } } // Returns true if n is right-truncatable  // else false  function rightTruPrime($n) { // Generating primes using Sieve  $isPrime = array_fill(0 $n + 1 true); sieveOfEratosthenes($n $isPrime); // Checking whether the number remains  // prime when the last ('right')  // digit is successively removed  while ($n) { if ($isPrime[$n]) $n = (int)($n / 10); else return false; } return true; } // Driver Code $n = 59399; if (rightTruPrime($n)) echo 'Yesn'; else echo 'Non'; // This code is contributed by mits ?> 
JavaScript
<script> // javascript code to check  // right-truncatable  // prime or not.  // Generate all prime  // numbers less than n.  function sieveOfEratosthenes(n isPrime)  {  // Initialize all entries of  // boolean array as true. A  // value in isPrime[i] will  // finally be false if i is  // Not a prime else true  // bool isPrime[n+1];  isPrime[0] = isPrime[1] = false;  for (let i = 2; i <= n; i++)  isPrime[i] = true;  for (let p = 2; p * p <= n; p++) {  // If isPrime[p] is not  // changed then it  // is a prime  if (isPrime[p] == true) {  // Update all multiples of p  for (let i = p * 2; i <= n; i += p)  isPrime[i] = false;  }  }  }  // Returns true if n is  // right-truncatable  // else false  function rightTruPrime(n)   {  // Generating primes using Sieve  let isPrime = new Array(n + 1).fill(false);  sieveOfEratosthenes(n isPrime);  // Checking whether the number  // remains prime when the last (right)  // digit is successively removed  while (n != 0) {  if (isPrime[n])  n = parseInt(n / 10);  else  return false;  }  return true;  }  // Driver program  var n = 59399;  if (rightTruPrime(n))  document.write('Yes');  else  document.write('No'); // This code is contributed by shikhasingrajput </script> 

Ieșire:  
 

Yes


Articol înrudit: Stânga-Troncabil Prime
Referinte:  
https://en.wikipedia.org/wiki/Truncatable_prime
 

Creați un test