persistence number

For example:

715 -> 35 -> 15 -> 5

88 -> 64 -> 24 -> 8

27 -> 14 -> 4
The number of products necessary to reach the single-digit number is called the persistence number of that integer. Thus 715 and 88 have a persistence number of 3, while 27 has persistence 2. Make a program to find the only two-digit number with persistence greater than 3?
I was able to come up with a rough idea and the code is below but it doesn't seem to work:

num2=0
num3=0
num4=0

num=input("what is your number?")

while num in range(10,100):
print 'step1'

num1=num%10*num/10

if num1-10>10:
print 'step2'
num2=num1%10*num1/10
elif num2-num1>10:
print 'step3'
num3=num2%10*num2/10
elif num3-num2>10:
print 'step4'
num4=num3%10*num3/10
elif num4-num3>10:
print 'step5'
print num4
else:
break
> and the code is below
that seems to be python. we are a c++ forum.

> but it doesn't seem to work
be more descriptive
Look at your first condition if num1-10>10:
consider that `num' is 88, `num1' would be 64 and the condition would be met. So you print 'step2' and are done with it, but 88 was an step3.

I'll recommend you to make a function that computes the range instead to try to do it in the loop.


By the way, I think that you need
(num1%10)*(num1//10)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;

int main(){
    int fd=0,ld=0; //first digit, last digit
    int t1,t2,t3;      //temp number
  for(int i=11;i<100;i++){
    fd=i%10;
    ld=i/10;
    t1=fd*ld;
    if(t1>10){
        fd=t1%10;
        ld=t1/10;
        t2=fd*ld;
        if(t2>10){
                fd=t2%10;
                ld=t2/10;
                t3=fd*ld;
                if(t3<10){
                    cout<<i<<"->" <<t1<<"->"<<t2<<"->"<<t3<<  " is persistence"<<endl;
                }
                    }

    }

  }
return 0;}


this is a c++ file so extention is .cpp.
if you use .c then change iostream to stdio.h and cout to printf.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
using namespace std;

int temp[10];
int f(int x,int d){
    int fd=0,ld=0;
    int t1,r;
    d=d+1;
    fd=x%10;
    ld=x/10;
    t1=fd*ld;
    temp[d]=t1;
    if(t1>10){
    r=f(t1,d);}
    else if(d>3){return 1; // set the persistence number
    }else{return 0;} 
    return r;
}

int main(){

  for(int i=11;i<100;i++){
     temp[0]=i;
int r=f(i,0);
if(r){
    for(int j=0;j<10;j++){
     if(temp[j]>0){
        cout<<temp[j]<<"->";
        temp[j]=0;}

    }
    cout<<endl;
}
  }
return 0;}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream> 
#include <string>

using namespace std;

int product( int );

int main()
{
	string integer = "";

	int number = 0;
	cin >> number;
	cout << product( number ) << endl;

	system( "pause" );
	return 0;
}

int product( int aNumber )
{
	int result = 1;
	string strNumber = std::to_string( aNumber );

	for ( int i = 0; i < strNumber.length(); i++ )
		result *= ( strNumber[i] - '0' );

	return result;
}


Maybe just extend this.
Last edited on
Topic archived. No new replies allowed.