i need to make a program that for example when given the number 879 must check whether the number is prime and whether all its digits subsequences are prime meaning 87,79,8,7,9 etc. So far i've made a function thats check whether a number is prime but have no idea on how to split a number into its digit subsequences.
-
Please check [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – sjsam Dec 01 '15 at 10:42
-
1this is homework, you should at least try it yourself. – Netwave Dec 01 '15 at 11:08
-
You should code your home work. And if you have problem, you can ask. – Viet Dec 01 '15 at 12:14
4 Answers
Let x
be the number. You could first determine the number of digits, n
, of x
. For example, if x = 543689
then n = 6
. This is easily determined by logarithms, for example (available via math.h
). Each digit in x
has an address ,i
, in the range 0,1,...,n-1
. It is natural to use a right-to-left ordering so that in the above example i=0
corresponds to the digit 9 rather than 5.
Set up a nested for loop that loops over all pairs of indices i,j
with 0 <= i <= j <n
. With each pass of the inner loop you need to get the number with starting index i
and ending index j
. You can do this in two steps
1) Let y = x % (10^(j+1))
. This will make y
equal to the substring whose leftmost digit is an index j
. For example, if x = 543689
and j = 4
then 10^5 = 100000
and 543689 % 100000 = 43689
-- the subsequence starting at index 4.
2) Divide y
by 10^i
-- this will throw away everything to the right of place i
. For example if i=2
and y = 43689
then y / 100 = 436
. Note that 436 is the part of 543689 with leftmost index 4 and rightmost index 2.
C doesn't have a built-in power operator. You could appropriately initialize int
vars to hold the powers 10^(j+1)
and 10^i
and update these powers appropriately (by multiplying by 10) in each pass through the loop.
Here is a Python implementation of these ideas (Python since I don't want to give C since this sounds like homework). The only thing that might not be self-explanatory is //
-- this is integer division in Python. In C you can just use /
-- assuming that you are dealing with int
variables:
x = 879
n = 3
for i in range(n):
for j in range(i,n):
y = x % 10**(j+1)
y = y // 10**i
print(y)
Output:
9
79
879
7
87
8

- 51,337
- 7
- 54
- 119
You might use this as well (source: find all subsequences of a number)
#include <stdio.h>
#define NUM 154
int main(void) {
int i,num=NUM,x,y,mask,digits=0,max=1;
while ( num != 0 ) {
num /= 10;
digits++;
}
for ( i = 1; i <= digits; i++ ) {
max *= 2;
}
printf("Subsequences are:\n");
for ( i = 1; i < max - 1 ; i++ ) {
mask = i;
x = 1;
num = NUM;
y=0;
while ( num != 0 ) {
if ( mask % 2 == 1 ) {
y += num % 10 * x;
x *= 10;
}
num /= 10;
mask /= 2;
}
printf("%d \n" , y);
}
return 0;
}
-
Returns 14 as the subsequence, 14 is not a valid subsequence of 154 as the digits 1,4 are not adjacent – LeDerp Apr 05 '17 at 05:57
You can use:
void chckdigits(int number)
{
int digits[10]= {0};
int i= 0;
while (number) {
digits[i]= number%10; // array is in reverse
number= number/10;
i++;
}
// now check all permutations
}

- 25,048
- 4
- 23
- 41
-
1question is about the sub sequences possible in a number not the digits present in a given number. Your solution will only give the digits present in the solution,it does not solve the whole problem. – Shvet Chakra Dec 01 '15 at 11:06
#include <stdio.h>
void isPrime(int n){
printf("%d is ...\n", n);
}
int main (void){
int theNumber = 879;
int base = 10;
int n, sub_n;
do {
for(n = theNumber; n >= base/10; n /= 10){
sub_n = n % base;
isPrime(sub_n);
}
base *= 10;
} while(sub_n != theNumber);
return 0;
}

- 39,699
- 7
- 33
- 70