+ 1

Right truncatable prime in c++

Truncatable prime is a prime number that when you successively remove digits from one end of the prime, you are left with a new prime number. An example of right truncatable prime is 2399->239->23->2 which remains prime The task is to find all of the 3digit right-truncatable prime numbers. - I used a for loop to first finde all of the prime numbers from 100 to 999 And then added two functions to check the truncatable primes But I couldn't write it correctly so the result is just prime numbers Any hints?

12th Apr 2021, 10:39 PM
Sahaarr
Sahaarr - avatar
11 Respostas
+ 2
Coder Kitten it helped a lot. Thanks🙏🏼🙏🏼
13th Apr 2021, 8:17 AM
Sahaarr
Sahaarr - avatar
+ 1
28th Jan 2023, 6:50 AM
Jarin P
Jarin P - avatar
0
Coder Kitten Thank you so much❤️ but I'm not allowed to use arrays. Can you help me do it with functions and loops?
13th Apr 2021, 5:22 AM
Sahaarr
Sahaarr - avatar
0
.. How to write code to print : 1 12 123 1234 12345 Plz i need help i'm beginner ☺️
20th Oct 2021, 9:39 AM
Mohamed Elamin
Mohamed Elamin - avatar
0
A right-truncatable prime is a prime number that remains prime when its digits are successively removed from the right side. Here's an example of how you can implement a program in C++ to find and display right-truncatable primes: #include <iostream> #include <vector> using namespace std; bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } bool isRightTruncatablePrime(int n) { while (n > 0) { if (!isPrime(n)) return false; n /= 10; } return true; } void displayRightTruncatablePrimes(int count) { vector<int> rightTruncatablePrimes; int num = 2; while (rightTruncatablePrimes.size() < count) { if (isRightTruncatablePrime(num)) { rightTruncatablePrimes.push_back(num); } num++; } cout << "Right-Truncatable Primes:"; for (int prime : rightTruncatablePrimes) { cout << " " << prime; } cout << endl; } int main() { int count; cout << "Enter the number of right-truncatable primes to find: "; cin >> count; displayRightTruncatablePrimes(count); return 0; } https://www.nexusiceland.me/
23rd Jun 2023, 6:56 AM
nexus iceland
nexus iceland - avatar
0
Here's how this code works: The isPrime function checks whether a number is prime by iterating from 2 up to the square root of the number and checking for divisibility. The isRightTruncatablePrime function takes a number and repeatedly divides it by 10 (removing the rightmost digit) while checking if each truncated number is prime using the isPrime function. The main function prompts the user to enter a number, calls isRightTruncatablePrime with the input number, and displays the result accordingly. Note: This code assumes that you have a C++ compiler installed and configured on your system. #include <iostream> #include <cmath> using namespace std; bool isPrime(int num) { if (num <= 1) return false; int sqrtNum = sqrt(num); for (int i = 2; i <= sqrtNum; i++) { if (num % i == 0) return false; } return true; } bool isRightTruncatablePrime(int num) { while (num > 0) { if (!isPrime(num)) return false; num /= 10; } return true; } int main() { int num; cout << "Enter a number: "; cin >> num; if (isRightTruncatablePrime(num)) cout << num << " is a right-truncatable prime." << endl; else cout << num << " is not a right-truncatable prime." << endl; return 0; } https://myhdfs.live/
23rd Jun 2023, 12:53 PM
myhdfs live
myhdfs live - avatar