The only built in function allowed are len() and range() and it has to be solved
ID: 3851716 • Letter: T
Question
The only built in function allowed are len() and range() and it has to be solved with loops or list.
truncatable_prime (n): check whether a number is a "right-truncatable prime" and return bool result. A right-truncatable prime is a prime which remains prime when the last (rightmost) digit is successively removed. (hint: how can you remove the last digit?...) o Assume: n is a positive integer. o Restrictions: no extra restrictions o truncatable_prime (2) truncatable_prime (113) truncatable_prime (5939) True False True #113 and 11 are primes, #5939 ,593, 59,and but I is not 5 are all primes histogram(xs): Given a list of integers named xs, return a string which, when printed, shows a histogram corresponding to the integers. Use lowercase "o" as the display symbol o Assume: xs is a list of non-negative integers; xs could be empty o Restrictions: none currently stated o histogram([3,10,0,5]) o histogram([]) 'ooo!noooooooooo!nInooooolniExplanation / Answer
Program has been implemented in java
In main() method all test cases, which you have provided, has been tested. You can run main().
TruncatablePrimeAndHistogram.java
import java.util.List;
import java.util.ArrayList;
public class TruncatablePrimeAndHistogram
{
public static void main(String a[])
{
List<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(10);
list.add(0);
list.add(5);
histogram(list);
System.out.println(truncatable_prime(2));
System.out.println(truncatable_prime(113));
System.out.println(truncatable_prime(5939));
}
public static boolean truncatable_prime(int number)
{
while(isPrime(number))
{
//removeLastDigit() removes last digit of given number and returns remained number
number = removeLastDigit(number);
//when single digit is given as number(input to this function), if removeLastDigit() executed on it, it will return 0.
//so, if result is 0 means number is truncatable_prime.
//so return true.
if(number==0)
return true;
}
return false;
}
public static void histogram(List<Integer> xs)
{
for(int i:xs)
{
for(int j=1;j<=i;j++)
System.out.print("o");
System.out.println();
}
}
public static boolean isPrime(int number)
{
//if given number is -ve or 0 or 1, then it will never be prime.
if(number<=1)
return false;
//by default we assume number is prime.
boolean isPrime = true;
//checking for a i which divides number evenly, so that we prove number is not prime
for(int i=2;i<number;i++)
{
if(number%i==0)
{
isPrime=false;
break;
}
}
return isPrime;
}
//removeLastDigit() function will return a number from which last digit is removed.
public static int removeLastDigit(int number){
return (int)number/10;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.