24 Computer -- Computer System

ask mattrab Visit www.askmattrab.com for more academic resources.

Largest palindrome product

Note: The following program is written in python.

This is the solution of the Q.No.4 of projecteuler.net site.
A palindromic number reads the same both ways.

eg. 121, 1221, 12344321 (All these numbers mean the same when reading from start to end and end to starting)

The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.    

The question is :
Find the largest palindrome made from the product of two 3-digit numbers.

Solution : 

num1 = 100           # 2 -digit numbers can be ignorednum2 = 100list1 = []while num1 < 1000:    while num2 < 1000:
        product = num1 * num2        string_list = str(product)        palindrome = string_list[::-1]        if string_list == palindrome:            list1.append(product)            num2 += 1        else:            num2 += 1    num1 +=1    num2 = 100
print(max(list1))

Discussions

subash neupane asked almost 4 years ago

Need in java🤔 I need the solution in java bro: The Veltech teams has been assigned the task of creating user ids for all the students for attending online exam. Also the Veltech has designed a process for generating the user id using the students first name, last name, district PIN code and a number N. The process defined by veltech is as below: Step 1: Compare the length of first name and last name of the student. The one that is shorter will be called Smaller name and the one that is longer will be called the Longer name. If both first name and last name are the equal length, then the name that appears earlier in alphabetical order will be called Smaller name and the name that appears later in alphabetical order will be called the Longer name. Step 2: The user id should be generated as below: Last letter of smaller name+ Entire word of Longer name+ Digit at position N in the PIN when traversing PIN from left to right+ Digit at position N in the PIN when traversing the PIN from right to left. Step 3: Toggle the alphabets of the user id generated in step-2 i.e upper-case alphabets should become lowercase and lower-case alphabets should become upper-case. You are part of veltechs team and youre requested to write a method to generate participants user id using the above rules. Input Format: Input1 is First_Name Input2 is Last_Name Input3 is PIN Input4 is the number N Constraint: The value of N (input 4) will always be less than or equal to the number of digits in the PIN. Input Test Case: 0 If the students details are as follows: First_Name= Rajiv Last_Name= Roy PIN=560037 N=6 Output Test Case 0: The user id will be=Last letter of smaller name+ Entire word of Longer name+ Digit at position N in the PIN when traversing PIN from left to right+ Digit at position N in the PIN when traversing the PIN from right to left = y+ Rajiv+7+5 Therefore user id= yRajiv75 After toggle the alphabets, user-id=YrAJIV75 Input Test Case: 1 First_Name= Manoj Last_Name= Kumar PIN=561327 N=2 Output Test Case 1: The user id will be=Last letter of smaller name+ Entire word of Longer name+ Digit at position N in the PIN when traversing PIN from left to right+ Digit at position N in the PIN when traversing the PIN from right to left =r+Manoj+6+2 =rManoj62 After toggle the alphabets=RmANOJ62

Dura G answered about 3 years ago

Hello Subash!

Here is the solution for the question you are asking for, I solved it in procedural way but if you are among the one who prefer OOP style then you can still ask it for me cause I have solved it from both methods but here I am just going to leave procedural one....

//author:Manish Acharya

import java.util.Scanner;

import java.util.*;


public class idgenerator {


public static void main(String[] args) {

String small_name="", long_name="", new_small_name="", new_long_name="";

char lr='a', rl='b';

int k=0, compare=0;

String id, reverse="";

Scanner userinput = new Scanner(System.in);

System.out.println("Enter the first name:");

String first_name = userinput.nextLine();

System.out.println("Enter the last name:");

String last_name = userinput.nextLine();

System.out.println("Enter the PIN:");

String pin = userinput.nextLine();

System.out.println("Enter any number N:");

System.out.println("Note: The given number should be less than or equal to PIN in length:");

int number = userinput.nextInt();

//checking part

if(number<=pin.length()) {

if(first_name.length() < last_name.length()) {

small_name = first_name;

long_name = last_name;

}

else if(first_name.length() > last_name.length()) {

long_name = first_name;

small_name = last_name;

}

else if(first_name.length() == last_name.length()) {

compare = first_name.compareTo(last_name);

if(compare<0) {

small_name = first_name;

long_name = last_name;

System.out.println("yes");

}

else if(compare>0){

long_name = first_name;

small_name = last_name;

}

else {

System.out.println("They are same!");

}

}

else {

System.out.println("Caught an unknown error!");

}

}

else {

System.out.println("Invalid number entered!");

}

//lowercase to uppercase and vice versa

for(int i=0; i<small_name.length(); i++) {

char a = small_name.charAt(i);

if(a >= 97 && a <= 123)

        {

           a = Character.toUpperCase(a); 

        }

        else if(a >= 65 && a <= 96)

        {

        a = Character.toLowerCase(a);

        }

new_small_name = new_small_name + a;

}

for(int i=0; i<long_name.length(); i++) {

char a = long_name.charAt(i);

if(a >= 97 && a <= 123)

        {

           a = Character.toUpperCase(a); 

        }

        else if(a >= 65 && a <= 96)

        {

        a = Character.toLowerCase(a);

        }

new_long_name = new_long_name + a;

}

//getting number from left to right and vice versa

for(int j=0; j<number; j++) {

lr = pin.charAt(j);

}

for(int j=pin.length()-1; j>=0; j--) {

reverse = reverse + pin.charAt(j);

}

for(int j=0; j<number; j++) {

rl = reverse.charAt(j);

}

//printing out id

System.out.println("Your ID is:" + new_small_name.charAt(new_small_name.length()-1) + new_long_name + lr + rl);

}


}


Close Open App