Check if Number is Twisted Prime

Factors are the numbers that can divide any given integer completely. Prime numbers have 2 factors only, 1 and itself. There are certain observations regarding factors.

  • 1 and the digit itself will always be the factor.
  • Factors always come in pairs, for example, 1*60, 2*30, 3*20, etc
  • The largest factor of any digit is the digit itself.
  • 2nd largest factor will be N/2, the 3rd largest factor is N/3 and the 4th largest factor is N/4, and so on.
  • Factors will always be less than the square root of the number.

A number is said to be Twisted Prime if it is a prime number and its reverse is also a prime number.

package org.wesome.dsalgo;

public class TwistedPrime {
    public static boolean checkTwistedPrime(int number) {
        boolean isNumberPrime = isPrime(number);
        if (isNumberPrime) {
            int reverseNumber = findIntegerReverse(number, 0);
            boolean isReversePrime = isPrime(reverseNumber);
            if (isReversePrime) {
                System.out.println("number " + number + " and its reverse Number = " + reverseNumber + " both are prime, hence its a twisted prime number");
                return true;
            } else {
                System.out.println("number " + number + "is prime but its reverseNumber = " + reverseNumber + " is not prime, hence its a not a twisted prime number");
                return false;
            }
        }
        System.out.println(number + " is not prime");
        return false;
    }


    private static int findIntegerReverse(int number, int integer) {
        while (number != 0) {
            integer = integer * 10 + (number % 10);
            return findIntegerReverse(number / 10, integer);
        }
        return integer;
    }

    public static boolean isPrime(int number) {
        if (number <= 1) {
            System.out.println("1 is the smallest prime number");
            return false;
        }
        for (int i = 2; i * i <= number; i++) {
            if (number % 2 == 0) {
                return false;
            }
        }
        return true;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.TwistedPrime.checkTwistedPrime;

public class TwistedPrimeTest {
    @Test
    void TwistedPrimeTest1() {
        Assertions.assertTrue(checkTwistedPrime(13));
    }

    @Test
    void TwistedPrimeTest2() {
        Assertions.assertTrue(checkTwistedPrime(31));
    }

    @Test
    void TwistedPrimeTest3() {
        Assertions.assertFalse(checkTwistedPrime(1));
    }

    @Test
    void TwistedPrimeTest4() {
        Assertions.assertTrue(checkTwistedPrime(3));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on