Converting KB to MB, GB, TB dynamically - java

public String size(int size){
String hrSize = "";
int k = size;
double m = size/1024;
double g = size/1048576;
double t = size/1073741824;
DecimalFormat dec = new DecimalFormat("0.00");
if (k>0)
{
hrSize = dec.format(k).concat("KB");
}
if (m>0)
{
hrSize = dec.format(m).concat("MB");
}
if (g>0)
{
hrSize = dec.format(g).concat("GB");
}
if (t>0)
{
hrSize = dec.format(t).concat("TB");
}
return hrSize;
}
This is a method that should return size in GB,MB, KB or TB. Input value is in KB.
for example result for 1245 should be like 1.21MB but what I get is 1.00MB.

A modified version. Only calls format once. Includes "Bytes".
public static String formatFileSize(long size) {
String hrSize = null;
double b = size;
double k = size/1024.0;
double m = ((size/1024.0)/1024.0);
double g = (((size/1024.0)/1024.0)/1024.0);
double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0);
DecimalFormat dec = new DecimalFormat("0.00");
if ( t>1 ) {
hrSize = dec.format(t).concat(" TB");
} else if ( g>1 ) {
hrSize = dec.format(g).concat(" GB");
} else if ( m>1 ) {
hrSize = dec.format(m).concat(" MB");
} else if ( k>1 ) {
hrSize = dec.format(k).concat(" KB");
} else {
hrSize = dec.format(b).concat(" Bytes");
}
return hrSize;
}

You are performing integer division. So the result of division is also integer. And fractional part is truncated.
so, 1245 / 1024 = 1
Change your division to floating point division: -
double m = size/1024.0;
double g = size/1048576.0;
double t = size/1073741824.0;
Also, your comparison is faulty. You should do the comparison with 1.
if (m > 1), if (t > 1), if (g > 1)
Ideally I would change your comparison to: -
if (t > 1) {
hrSize = dec.format(t).concat("TB");
} else if (g > 1) {
hrSize = dec.format(g).concat("GB");
} else if (m > 1) {
hrSize = dec.format(m).concat("MB");
} else {
hrSize = dec.format(size).concat("KB");
}
You need to compare with the higher unit first, and then move to the lower one.

I love this:
public static String getDynamicSpace(long diskSpaceUsed)
{
if (diskSpaceUsed <= 0) {
return "0";
}
final String[] units = new String[] { "B", "KiB", "MiB", "GiB", "TiB" };
int digitGroups = (int) (Math.log10(diskSpaceUsed) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(diskSpaceUsed / Math.pow(1024, digitGroups))
+ " " + units[digitGroups];
}

The problem is that you're using integer division. Change your code to:
double m = size/1024.0;
double g = size/1048576.0;
double t = size/1073741824.0;
In your original code, double m = size/1024 would divide the integer size by 1024, truncate the result to an integer, and only then convert it to double. This is why the fractional part was getting lost.

You are performing integer division,
i.e., 31/15 will result in 2, not 2.whatever
just append the number with D or d which denotes it as a double and you will be fine
double m = size/1024D;
double g = size/1048576D;
double t = size/1073741824D;

Its not easy to get that right. Rohit Jain mentioned the integer operation. Also rounding can be an issue, as always rounding down may not be desirable. I would advise to go for an available solution like in the triava library.
It can format numbers with arbitrary precision, in 3 different systems (SI, IEC, JEDEC) and various output options. Here are some code examples from the triava unit tests:
UnitFormatter.formatAsUnit(1126, UnitSystem.SI, "B");
// = "1.13kB"
UnitFormatter.formatAsUnit(2094, UnitSystem.IEC, "B");
// = "2.04KiB"
Printing exact kilo, mega values (here with W = Watt):
UnitFormatter.formatAsUnits(12_000_678, UnitSystem.SI, "W", ", ");
// = "12MW, 678W"
You can pass a DecimalFormat to customize the output:
UnitFormatter.formatAsUnit(2085, UnitSystem.IEC, "B", new DecimalFormat("0.0000"));
// = "2.0361KiB"
For arbitrary operations on kilo or mega values, you can split them into components:
UnitComponent uc = new UnitComponent(123_345_567_789L, UnitSystem.SI);
int kilos = uc.kilo(); // 567
int gigas = uc.giga(); // 123

class ConverterUtils{
public static void main(String[] args) {
System.out.println(getSize(1234567));
}
public static String getSize(long size) {
String s = "";
double kb = size / 1024;
double mb = kb / 1024;
double gb = mb / 1024;
double tb = gb / 1024;
if(size < 1024L) {
s = size + " Bytes";
} else if(size >= 1024 && size < (1024L * 1024)) {
s = String.format("%.2f", kb) + " KB";
} else if(size >= (1024L * 1024) && size < (1024L * 1024 * 1024)) {
s = String.format("%.2f", mb) + " MB";
} else if(size >= (1024L * 1024 * 1024) && size < (1024L * 1024 * 1024 * 1024)) {
s = String.format("%.2f", gb) + " GB";
} else if(size >= (1024L * 1024 * 1024 * 1024)) {
s = String.format("%.2f", tb) + " TB";
}
return s;
}
}
To better understand - https://www.techspot.com/news/68482-quickly-convert-between-storage-size-units-kb-mb.html

public class FileSizeCalculator {
String[] fileSizeUnits = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
public static void main(String[] args) {
FileSizeCalculator fs = new FileSizeCalculator();
String properFileSize = fs.calculateProperFileSize(2362232012l);
System.out.println("Proper file size: " + properFileSize);
}
public String calculateProperFileSize(long noOfBytes){
String sizeToReturn = "";// = FileUtils.byteCountToDisplaySize(bytes), unit = "";
double bytes = noOfBytes;
int index = 0;
for(index = 0; index < fileSizeUnits.length; index++){
if(bytes < 1024){
break;
}
bytes = bytes / 1024;
}
sizeToReturn = String.valueOf(bytes) + " " + fileSizeUnits[index];
return sizeToReturn;
}
}
Just add more file unit (if any missing), and you will see unit size up to that unit (if your file has that much length)

My basic version (you CAN define some constants instead of computing POW all the time):
public static String GetFolderSizeHuman(long aBytes)
{
if (aBytes < 1024 * 1024)
return aBytes + " KB";
else if (aBytes < Math.pow(2, 20) * 1024)
return (int) aBytes / Math.pow(2, 20) + " MB";
else if (aBytes < Math.pow(2, 30) * 1024 )
return kGbTbFormatter.format(aBytes / Math.pow(2, 30)) + " GB";
else if (aBytes < Math.pow(2, 40) * 1024)
return kGbTbFormatter.format(aBytes / Math.pow(2, 40)) + " TB";
else return "N/A (1TB?)";
}

bickster's answer works quite alright but the problem with it is that it returns results like 45.00 Bytes and 12.00 KB. In my opinion, the last decimal digits should be removed if they are zeros. So instead of 45.00 Bytes and 12.00 KB, you get 45 B and 12 KB (notice that Bytes has been changed to B. This is just for uniformity since we have KB, MB etc and not Kilobytes, Megabytes etc).
private boolean isDouble(double value) {
if (value % 1 == 0) {
Log.d(TAG, "value is " + value + " and is not double");
return false;
} else {
Log.d(TAG, "value is " + value + " and is double");
return true;
}
}
The above method simply checks if the value has zeros as decimal digits.
private String formatFileSize(long size) {
String hrSize = null;
double b = size;
double k = size/1024.0;
double m = ((size/1024.0)/1024.0);
double g = (((size/1024.0)/1024.0)/1024.0);
double t = ((((size/1024.0)/1024.0)/1024.0)/1024.0);
DecimalFormat dec1 = new DecimalFormat("0.00");
DecimalFormat dec2 = new DecimalFormat("0");
if (t>1) {
hrSize = isDouble(t) ? dec1.format(t).concat(" TB") : dec2.format(t).concat(" TB");
} else if (g>1) {
hrSize = isDouble(g) ? dec1.format(g).concat(" GB") : dec2.format(g).concat(" GB");
} else if (m>1) {
hrSize = isDouble(m) ? dec1.format(m).concat(" MB") : dec2.format(m).concat(" MB");
} else if (k>1) {
hrSize = isDouble(k) ? dec1.format(k).concat(" KB") : dec2.format(k).concat(" KB");
} else {
hrSize = isDouble(b) ? dec1.format(b).concat(" B") : dec2.format(b).concat(" B");
}
return hrSize;
}

Related

how can use remainder operator to get remaining KB when converting into MB?

public class MegaByteConverter {
public static void main(String[] args) {
printMegaBytesAndKiloBytes(2500);
}
public static void printMegaBytesAndKiloBytes(int kiloByte) {
if (kiloByte < 0) {
System.out.println("Invalid Value");
}
System.out.println(kiloByte + " KB =" +
kiloByte / 1024 + " MB AND " + (kiloByte / 1024) % kiloByte + " KB");
}
}
I'm not able to get remaining KBs. there's something wrong with (kiloByte/1024) % kiloByte.
There should be no need to perform (kiloByte/1024) prior to the % operation. That is the cause of your problem.
What you have:
int kiloByte = 2500;
int megaByte = (kiloBytes / 1024); // 2500 / 1024 == 2
int remainder = megaByte % 1024; // 2 % 1024 == 2 which is not what you want
compared to:
int kiloByte = 2500;
int megaByte = (kiloBytes / 1024); // == 2 (MB)
int remainder = kiloBytes % 1024; // == 452 (KB) which is your desired outcome

Rounding longs and reformatting

I'm trying to figure out how to reformat a long value that I get from an API. The long value represents a stock's market cap so naturally it varies, but essentially what I would like is to round up and abbreviate.
I've found on here a code to abbreviate the number down depending on how high it is, but I can't figure out how to keep decimals.
For example if I query "NFLX" I get back "117173403648". With the code I would get back 117B. What I would like the output to be is "117.17B".
I have tried Math.round() function before I abbreviate the number, but it does not seem to work the way I want it to, nor does DecimalFormat. Surely I'm using both wrong.
here is the code I found and modified on SO:
public static String abbreviateNumber(long marketCap) {
long temp = marketCap / 1000000000;
if (temp > 0) {
return temp + "B";
}
temp = marketCap / 1000000;
if (temp > 0) {
return temp + "M";
}
temp = marketCap / 1000;
if (temp > 0) {
return temp + "K";
}
return String.valueOf(marketCap);
}
Using this the output of marketCap is 117B, but I would like to have 2 decimals, and still keep this abbreviation method.
Thanks, any help is greatly appreciated. I'm new to programming and figuring it out as I go.
How about something like this:
public static String abbreviateNumber(long marketCap) {
int len = String.valueOf(marketCap).length();
return len > 9 ? String.format("%.2f", marketCap/Math.pow(10, 9))+"B" :
len > 6 ? String.format("%.2f", marketCap/Math.pow(10, 6))+"M" :
len > 3 ? String.format("%.2f", marketCap/Math.pow(10, 3))+"K" :
String.valueOf(marketCap);
}
Use DecimalFormat and double
DecimalFormat format = new DecimalFormat("#0.00B");
long marketCap = 117173403648L;
double value = marketCap / 1000000000.0;
System.out.println(format.format(value));
117.17B
You should use DecimalFormat class present in java.text package.
Your code would look like this:
public static String abbreviateNumber(long marketCap) {
double temp = marketCap / 1000000000;
DecimalFormat f = new DecimalFormat("##.##");
if (temp > 0) {
return f.format(marketCap / 1000000000d) + "B";
}
temp = marketCap / 1000000;
if (temp > 0) {
return f.format(marketCap / 1000000d) + "M";
}
temp = marketCap / 1000;
if (temp > 0) {
return f.format(marketCap / 1000d) + "K";
}
return String.valueOf(marketCap);
}
note
Not optimal & clean solution. Just updated the answer to working one.
With a little change to the code where the division happens, decimals retention can be achieved. And on top that String.format("%.2f", input) can be used for decimal clipping.
public static String abbreviateNumber(long marketCap) {
//for billion -1000 million above
double temp = ((double)(marketCap / 1000000000.0));
if (temp > 1) {
return String.format("%.2f", temp) + "B";
}
//for million formatting
temp = ((double)(marketCap / 1000000.0));
if (temp > 1) {
return String.format("%.2f", temp) + "M";
}
//for thousands formatting
//if K representation only when it reaches at least 1K make temp > 1
temp = ((double)(marketCap / 1000.0));
if (temp > 0) {
return String.format("%.2f", temp) + "K";
}
//this will not be reached unless a negative number
return String.valueOf(marketCap);
}
For "117173403648l" as input:
117.17B

-1 calculation bug in java [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I took this problem from an experienced programmer to prepare for a programming test.
Problem:
Given the amount of a purchase and the amount given as payment, calculate the number and type of each denomination of currency returned as change. Change is always given with the most of the highest denominations first. The denominations of paper currency used will be $100, $50, $20, $10, $5 and $1. The denominations of the coins will be $0.25, $0.10, $0.05 and $0.01.
INPUT: There will be 5 lines of input. Each line will contain 2 rational numbers representing the amount of a purchase and the amount given as payment.
OUTPUT: For each input line print the change using the number of non-zero denominations used in order of their magnitude from high to low. If no change is due print NONE. Sample Output #1 represents: 1-$5, 2-$1, 1-$.25, 2-$.10 and 2-$.01.
SAMPLE INPUT SAMPLE OUTPUT
1. 12.53, 20.00 1. 121221
2. 11.33, 15.00 2. 32112
3. 35.64, 50.00 3. 14111
4. 72.67, 100.00 4. 112113
5. 106.68, 200.00 5. 123112
I decided to create two programs: a long, easier-to-follow-one and a condensed version.
Here is the long one:
import java.util.*;
public class Prac13CorrectChange {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int A = 1; A <= 5; A++) {
System.out
.println("What is the amount of your purchase and the amount given as your payment?");
String input = in.nextLine();
String[] data = input.split(",");
double $amount = Double.parseDouble(data[0]);
double price = Double.parseDouble(data[1]);
double change = price - $amount;
int ans1 = ((int) (change / 100));
change = change - (ans1 * 100);
int ans2 = ((int) (change / 50));
change = change - (ans2 * 50);
int ans3 = ((int) (change / 20));
change = change - (ans3 * 20);
int ans4 = ((int) (change / 10));
change = change - (ans4 * 10);
int ans5 = ((int) (change / 5));
change = change - (ans5 * 5);
int ans6 = ((int) (change / 1));
change = change - (ans6 * 1);
int ans7 = ((int) (change / .25));
change = change - (ans7 * .25);
int ans8 = ((int) (change / .10));
change = change - (ans8 * .10);
int ans9 = ((int) (change / .05));
change = change - (ans9 * .05);
int ans10 = ((int) (change / .01));
change = change - (ans10 * .01);
String answer = "";
if (ans1 != 0) {
answer = answer + Integer.toString(ans1);
}
if (ans2 != 0) {
answer = answer + Integer.toString(ans2);
}
if (ans3 != 0) {
answer = answer + Integer.toString(ans3);
}
if (ans4 != 0) {
answer = answer + Integer.toString(ans4);
}
if (ans5 != 0) {
answer = answer + Integer.toString(ans5);
}
if (ans6 != 0) {
answer = answer + Integer.toString(ans6);
}
if (ans7 != 0) {
answer = answer + Integer.toString(ans7);
}
if (ans8 != 0) {
answer = answer + Integer.toString(ans8);
}
if (ans9 != 0) {
answer = answer + Integer.toString(ans9);
}
if (ans10 != 0) {
answer = answer + Integer.toString(ans10);
}
System.out.println(answer);
}
}
}
And here is the condensed version.
import java.util.Scanner;
public class Prac15CorrectChangeShort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int A = 1; A <= 5; A++) {
System.out
.println("What is the amount of your purchase and the amount given as your payment?");
String input = in.nextLine();
String[] data = input.split(",");
double $amount = Double.parseDouble(data[0]);
double price = Double.parseDouble(data[1]);
double change = price - $amount;
int ans[] = new int[10];
double money[] = { 100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01 };
for (int looptimes = 0; looptimes < 10; looptimes++) {
ans[looptimes] = ((int) (change / money[looptimes]));
change = change - (ans[looptimes] * money[looptimes]);
}
for (int looptimes = 0; looptimes < 10; looptimes++) {
if (ans[looptimes] != 0) {
System.out.print(ans[looptimes]);
}
}
System.out.print('\n');
}
}
}
The main problem I am having is that, no matter the debugging I do, it is giving me incorrect outputs #2-5. It gives me 1 less for the last digit. Neither the programmer who gave me this problem nor I can find any reason for this occurrence.
Please help.
Looks like a floating point error.
Read this
You can use BigDecimal instead of double when calculating the change.
BigDecimal change = BigDecimal.valueOf(price - $amount);

Binary to Decimal Recursive Method

I am trying to figure out method to convert from binary to decimal via recursion. My method is constructed as follows from the calculations done on paper. If binary was "1100" (12 in decimal), then
/*1100 ---> 12
(1)*2 + "100"
(2 + 1)*2 + "00"
(6 + 0)*2 + "0"
(12 + 0) + "" */
I've been stuck on this for awhile. Here is what I have come up with. I appreciate any ideas. Thanks.
int binary2decimal(String b)
{
if ("".equals(b)) //base case
return 0;
else //general case
{ if (b.length() == 1)
return (b.charAt(0) - '0') + binary2decimal(b.substring(1));
else
return (b.charAt(0) - '0')*2 + binary2decimal(b.substring(1));
}
}
this should work :
class binary {
public static void main(String[] args)
{
int myBinary = binary2decimal("1100");
System.out.println(myBinary);
//System.out.println(binary2decimal("1100"));
}
static int binary2decimal(String b) {
int len = b.length();
if (len == 0) return 0;
String now = b.substring(0,1);
String later = b.substring(1);
return Integer.parseInt(now) * (int)Math.pow(2, len-1) + binary2decimal(later);
}
}
with output:
OUTPUT : 12
int binary2decimal(String b)
{
int dec = 0;
int len = b.length();
if (len >= 1)
{
dec = (b.charAt(len-1) - '0');
if (len > 1) dec += binary2decimal(b.substring(0,len-1))<<1;
}
return dec;
}
The whole point is to recurse backwards (i.e from LSB to MSB)
edit binary2decimal(b.substring(0,len-2)) needs to go up to len-1, fixed

Find the largest palindrome made from the product of two 3-digit numbers

package testing.project;
public class PalindromeThreeDigits {
public static void main(String[] args) {
int value = 0;
for(int i = 100;i <=999;i++)
{
for(int j = i;j <=999;j++)
{
int value1 = i * j;
StringBuilder sb1 = new StringBuilder(""+value1);
String sb2 = ""+value1;
sb1.reverse();
if(sb2.equals(sb1.toString()) && value<value1) {
value = value1;
}
}
}
System.out.println(value);
}
}
This is the code that I wrote in Java... Is there any efficient way other than this.. And can we optimize this code more??
We suppose the largest such palindrome will have six digits rather than five, because 143*777 = 111111 is a palindrome.
As noted elsewhere, a 6-digit base-10 palindrome abccba is a multiple of 11. This is true because a*100001 + b*010010 + c*001100 is equal to 11*a*9091 + 11*b*910 + 11*c*100. So, in our inner loop we can decrease n by steps of 11 if m is not a multiple of 11.
We are trying to find the largest palindrome under a million that is a product of two 3-digit numbers. To find a large result, we try large divisors first:
We step m downwards from 999, by 1's;
Run n down from 999 by 1's (if 11 divides m, or 9% of the time) or from 990 by 11's (if 11 doesn't divide m, or 91% of the time).
We keep track of the largest palindrome found so far in variable q. Suppose q = r·s with r <= s. We usually have m < r <= s. We require m·n > q or n >= q/m. As larger palindromes are found, the range of n gets more restricted, for two reasons: q gets larger, m gets smaller.
The inner loop of attached program executes only 506 times, vs the ~ 810000 times the naive program used.
#include <stdlib.h>
#include <stdio.h>
int main(void) {
enum { A=100000, B=10000, C=1000, c=100, b=10, a=1, T=10 };
int m, n, p, q=111111, r=143, s=777;
int nDel, nLo, nHi, inner=0, n11=(999/11)*11;
for (m=999; m>99; --m) {
nHi = n11; nDel = 11;
if (m%11==0) {
nHi = 999; nDel = 1;
}
nLo = q/m-1;
if (nLo < m) nLo = m-1;
for (n=nHi; n>nLo; n -= nDel) {
++inner;
// Check if p = product is a palindrome
p = m * n;
if (p%T==p/A && (p/B)%T==(p/b)%T && (p/C)%T==(p/c)%T) {
q=p; r=m; s=n;
printf ("%d at %d * %d\n", q, r, s);
break; // We're done with this value of m
}
}
}
printf ("Final result: %d at %d * %d inner=%d\n", q, r, s, inner);
return 0;
}
Note, the program is in C but same techniques will work in Java.
What I would do:
Start at 999, working my way backwards to 998, 997, etc
Create the palindrome for my current number.
Determine the prime factorization of this number (not all that expensive if you have a pre-generated list of primes.
Work through this prime factorization list to determine if I can use a combination of the factors to make 2 3 digit numbers.
Some code:
int[] primes = new int[] {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,,127,131,137,139,149,151,157,163,167,173,
179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,
283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,
419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,
547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,
661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,
811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,
947,953,967,971,977,983,991,997};
for(int i = 999; i >= 100; i--) {
String palstr = String.valueOf(i) + (new StringBuilder().append(i).reverse());
int pal = Integer.parseInt(pal);
int[] factors = new int[20]; // cannot have more than 20 factors
int remainder = pal;
int facpos = 0;
primeloop:
for(int p = 0; p < primes.length; i++) {
while(remainder % p == 0) {
factors[facpos++] = p;
remainder /= p;
if(remainder < p) break primeloop;
}
}
// now to do the combinations here
}
We can translate the task into the language of mathematics.
For a short start, we use characters as digits:
abc * xyz = n
abc is a 3-digit number, and we deconstruct it as 100*a+10*b+c
xyz is a 3-digit number, and we deconstruct it as 100*x+10*y+z
Now we have two mathematical expressions, and can define a,b,c,x,y,z as € of {0..9}.
It is more precise to define a and x as of element from {1..9}, not {0..9}, because 097 isn't really a 3-digit number, is it?
Ok.
If we want to produce a big number, we should try to reach a 9......-Number, and since it shall be palindromic, it has to be of the pattern 9....9. If the last digit is a 9, then from
(100*a + 10*b + c) * (100*x + 10*y + z)
follows that z*c has to lead to a number, ending in digit 9 - all other calculations don't infect the last digit.
So c and z have to be from (1,3,7,9) because (1*9=9, 9*1=9, 3*3=9, 7*7=49).
Now some code (Scala):
val n = (0 to 9)
val m = n.tail // 1 to 9
val niners = Seq (1, 3, 7, 9)
val highs = for (a <- m;
b <- n;
c <- niners;
x <- m;
y <- n;
z <- niners) yield ((100*a + 10*b + c) * (100*x + 10*y + z))
Then I would sort them by size, and starting with the biggest one, test them for being palindromic. So I would omit to test small numbers for being palindromic, because that might not be so cheap.
For aesthetic reasons, I wouldn't take a (toString.reverse == toString) approach, but a recursive divide and modulo solution, but on todays machines, it doesn't make much difference, does it?
// Make a list of digits from a number:
def digitize (z: Int, nums : List[Int] = Nil) : List[Int] =
if (z == 0) nums else digitize (z/10, z%10 :: nums)
/* for 342243, test 3...==...3 and then 4224.
Fails early for 123329 */
def palindromic (nums : List[Int]) : Boolean = nums match {
case Nil => true
case x :: Nil => true
case x :: y :: Nil => x == y
case x :: xs => x == xs.last && palindromic (xs.init) }
def palindrom (z: Int) = palindromic (digitize (z))
For serious performance considerations, I would test it against a toString/reverse/equals approach. Maybe it is worse. It shall fail early, but division and modulo aren't known to be the fastest operations, and I use them to make a List from the Int. It would work for BigInt or Long with few redeclarations, and works nice with Java; could be implemented in Java but look different there.
Okay, putting the things together:
highs.filter (_ > 900000) .sortWith (_ > _) find (palindrom)
res45: Option[Int] = Some(906609)
There where 835 numbers left > 900000, and it returns pretty fast, but I guess even more brute forcing isn't much slower.
Maybe there is a much more clever way to construct the highest palindrom, instead of searching for it.
One problem is: I didn't knew before, that there is a solution > 900000.
A very different approach would be, to produce big palindromes, and deconstruct their factors.
public class Pin
{
public static boolean isPalin(int num)
{
char[] val = (""+num).toCharArray();
for(int i=0;i<val.length;i++)
{
if(val[i] != val[val.length - i - 1])
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
for(int i=999;i>100;i--)
for(int j=999;j>100;j--)
{
int mul = j*i;
if(isPalin(mul))
{
System.out.printf("%d * %d = %d",i,j,mul);
return;
}
}
}
}
package ex;
public class Main {
public static void main(String[] args) {
int i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, flag = 0;
for (i = 999; i >= 100; i--) {
for (j = i; j >= 100; j--) {
k = i * j;
// System.out.println(k);
m = 0;
n = k;
while (n > 0) {
l = n % 10;
m = m * 10 + l;
n = n / 10;
}
if (m == k) {
System.out.println("pal " + k + " of " + i + " and" + j);
flag = 1;
break;
}
}
if (flag == 1) {
// System.out.println(k);
break;
}
}
}
}
A slightly different approach that can easily calculate the largest palindromic number made from the product of up to two 6-digit numbers.
The first part is to create a generator of palindrome numbers. So there is no need to check if a number is palindromic, the second part is a simple loop.
#include <memory>
#include <iostream>
#include <cmath>
using namespace std;
template <int N>
class PalindromeGenerator {
unique_ptr <int []> m_data;
bool m_hasnext;
public :
PalindromeGenerator():m_data(new int[N])
{
for(auto i=0;i<N;i++)
m_data[i]=9;
m_hasnext=true;
}
bool hasNext() const {return m_hasnext;}
long long int getnext()
{
long long int v=0;
long long int b=1;
for(int i=0;i<N;i++){
v+=m_data[i]*b;
b*=10;
}
for(int i=N-1;i>=0;i--){
v+=m_data[i]*b;
b*=10;
}
auto i=N-1;
while (i>=0)
{
if(m_data[i]>=1) {
m_data[i]--;
return v;
}
else
{
m_data[i]=9;
i--;
}
}
m_hasnext=false;
return v;
}
};
template<int N>
void findmaxPalindrome()
{
PalindromeGenerator<N> gen;
decltype(gen.getnext()) minv=static_cast<decltype(gen.getnext())> (pow(10,N-1));
decltype(gen.getnext()) maxv=static_cast<decltype(gen.getnext())> (pow(10,N)-1);
decltype(gen.getnext()) start=11*(maxv/11);
while(gen.hasNext())
{
auto v=gen.getnext();
for (decltype(gen.getnext()) i=start;i>minv;i-=11)
{
if (v%i==0)
{
auto r=v/i;
if (r>minv && r<maxv ){
cout<<"done:"<<v<<" "<<i<< "," <<r <<endl;
return ;
}
}
}
}
return ;
}
int main(int argc, char* argv[])
{
findmaxPalindrome<6>();
return 0;
}
You can use the fact that 11 is a multiple of the palindrome to cut down on the search space. We can get this since we can assume the palindrome will be 6 digits and >= 111111.
e.g. ( from projecteuler ;) )
P= xyzzyx = 100000x + 10000y + 1000z + 100z + 10y +x
P=100001x+10010y+1100z
P=11(9091x+910y+100z)
Check if i mod 11 != 0, then the j loop can be subtracted by 11 (starting at 990) since at least one of the two must be divisible by 11.
You can try the following which prints
999 * 979 * 989 = 967262769
largest palindrome= 967262769 took 0.015
public static void main(String... args) throws IOException, ParseException {
long start = System.nanoTime();
int largestPalindrome = 0;
for (int i = 999; i > 100; i--) {
LOOP:
for (int j = i; j > 100; j--) {
for (int k = j; k > 100; k++) {
int n = i * j * k;
if (n < largestPalindrome) continue LOOP;
if (isPalindrome(n)) {
System.out.println(i + " * " + j + " * " + k + " = " + n);
largestPalindrome = n;
}
}
}
}
long time = System.nanoTime() - start;
System.out.printf("largest palindrome= %d took %.3f seconds%n", largestPalindrome, time / 1e9);
}
private static boolean isPalindrome(int n) {
if (n >= 100 * 1000 * 1000) {
// 9 digits
return n % 10 == n / (100 * 1000 * 1000)
&& (n / 10 % 10) == (n / (10 * 1000 * 1000) % 10)
&& (n / 100 % 10) == (n / (1000 * 1000) % 10)
&& (n / 1000 % 10) == (n / (100 * 1000) % 10);
} else if (n >= 10 * 1000 * 1000) {
// 8 digits
return n % 10 == n / (10 * 1000 * 1000)
&& (n / 10 % 10) == (n / (1000 * 1000) % 10)
&& (n / 100 % 10) == (n / (100 * 1000) % 10)
&& (n / 1000 % 10) == (n / (10 * 1000) % 10);
} else if (n >= 1000 * 1000) {
// 7 digits
return n % 10 == n / (1000 * 1000)
&& (n / 10 % 10) == (n / (100 * 1000) % 10)
&& (n / 100 % 10) == (n / (10 * 1000) % 10);
} else throw new AssertionError();
}
i did this my way , but m not sure if this is the most efficient way of doing this .
package problems;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class P_4 {
/**
* #param args
* #throws IOException
*/
static int[] arry = new int[6];
static int[] arry2 = new int[6];
public static boolean chk()
{
for(int a=0;a<arry.length;a++)
if(arry[a]!=arry2[a])
return false;
return true;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int temp,z,i;
for(int x=999;x>100;x--)
for(int y=999;y>100;y--)
{
i=0;
z=x*y;
while(z>0)
{
temp=z%10;
z=z/10;
arry[i]=temp;
i++;
}
for(int k = arry.length;k>0;k--)
arry2[arry.length- k]=arry[k-1];
if(chk())
{
System.out.print("pelindrome = ");
for(int l=0;l<arry2.length;l++)
System.out.print(arry2[l]);
System.out.println(x);
System.out.println(y);
}
}
}
}
This is code in C, a little bit long, but gets the job done.:)
#include <stdio.h>
#include <stdlib.h>
/*
A palindromic number reads the same both ways. The largest palindrome made from the product of two
2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.*/
int palndr(int b)
{
int *x,*y,i=0,j=0,br=0;
int n;
n=b;
while(b!=0)
{
br++;
b/=10;
}
x=(int *)malloc(br*sizeof(int));
y=(int *)malloc(br*sizeof(int));
int br1=br;
while(n!=0)
{
x[i++]=y[--br]=n%10;
n/=10;
}
int ind = 1;
for(i=0;i<br1;i++)
if(x[i]!=y[i])
ind=0;
free(x);
free(y);
return ind;
}
int main()
{
int i,cek,cekmax=1;
int j;
for(i=100;i<=999;i++)
{
for(j=i;j<=999;j++)
{
cek=i*j;
if(palndr(cek))
{
if(pp>cekmax)
cekmax=cek;
}
}
}
printf("The largest palindrome is: %d\n\a",cekmax);
}
You can actually do it with Python, it's easy just take a look:
actualProduct = 0
highestPalindrome = 0
# Setting the numbers. In case it's two digit 10 and 99, in case is three digit 100 and 999, etc.
num1 = 100
num2 = 999
def isPalindrome(number):
number = str(number)
reversed = number[::-1]
if number==reversed:
return True
else:
return False
a = 0
b = 0
for i in range(num1,num2+1):
for j in range(num1,num2+1):
actualProduct = i * j
if (isPalindrome(actualProduct) and (highestPalindrome < actualProduct)):
highestPalindrome = actualProduct
a = i
b = j
print "Largest palindrome made from the product of two %d-digit numbers is [ %d ] made of %d * %d" % (len(str(num1)), highestPalindrome, a, b)
Since we are not cycling down both iterators (num1 and num2) at the same time, the first palindrome number we find will be the largest. We don’t need to test to see if the palindrome we found is the largest. This significantly reduces the time it takes to calculate.
package testing.project;
public class PalindromeThreeDigits {
public static void main(String[] args) {
int limit = 99;
int max = 999;
int num1 = max, num2, prod;
while(num1 > limit)
{
num2 = num1;
while(num2 > limit)
{
total = num1 * num2;
StringBuilder sb1 = new StringBuilder(""+prod);
String sb2 = ""+prod;
sb1.reverse();
if( sb2.equals(sb1.toString()) ) { //optimized here
//print and exit
}
num2--;
}
num1--;
}
}//end of main
}//end of class PalindromeThreeDigits
I tried the solution by Tobin joy and vickyhacks and both of them produce the result 580085 which is wrong here is my solution, though very clumsy:
import java.util.*;
class ProjEu4
{
public static void main(String [] args) throws Exception
{
int n=997;
ArrayList<Integer> al=new ArrayList<Integer>();
outerloop:
while(n>100){
int k=reverse(n);
int fin=n*1000+k;
al=findfactors(fin);
if(al.size()>=2)
{
for(int i=0;i<al.size();i++)
{
if(al.contains(fin/al.get(i))){
System.out.println(fin+" factors are:"+al.get(i)+","+fin/al.get(i));
break outerloop;}
}
}
n--;
}
}
private static ArrayList<Integer> findfactors(int fin)
{
ArrayList<Integer> al=new ArrayList<Integer>();
for(int i=100;i<=999;i++)
{
if(fin%i==0)
al.add(i);
}
return al;
}
private static int reverse(int number)
{
int reverse = 0;
while(number != 0){
reverse = (reverse*10)+(number%10);
number = number/10;
}
return reverse;
}
}
Most probably it is replication of one of the other solution but it looks simple owing to pythonified code ,even it is a bit brute-force.
def largest_palindrome():
largest_palindrome = 0;
for i in reversed(range(1,1000,1)):
for j in reversed(range(1, i+1, 1)):
num = i*j
if check_palindrome(str(num)) and num > largest_palindrome :
largest_palindrome = num
print "largest palindrome ", largest_palindrome
def check_palindrome(term):
rev_term = term[::-1]
return rev_term == term
What about : in python
>>> for i in range((999*999),(100*100), -1):
... if str(i) == str(i)[::-1]:
... print i
... break
...
997799
>>>
I believe there is a simpler approach: Examine palindromes descending from the largest product of two three digit numbers, selecting the first palindrome with two three digit factors.
Here is the Ruby code:
require './palindrome_range'
require './prime'
def get_3_digit_factors(n)
prime_factors = Prime.factors(n)
rf = [prime_factors.pop]
rf << prime_factors.shift while rf.inject(:*) < 100 || prime_factors.inject(:*) > 999
lf = prime_factors.inject(:*)
rf = rf.inject(:*)
lf < 100 || lf > 999 || rf < 100 || rf > 999 ? [] : [lf, rf]
end
def has_3_digit_factors(n)
return !get_3_digit_factors(n).empty?
end
pr = PalindromeRange.new(0, 999 * 999)
n = pr.downto.find {|n| has_3_digit_factors(n)}
puts "Found #{n} - Factors #{get_3_digit_factors(n).inspect}, #{Prime.factors(n).inspect}"
prime.rb:
class Prime
class<<self
# Collect all prime factors
# -- Primes greater than 3 follow the form of (6n +/- 1)
# Being of the form 6n +/- 1 does not mean it is prime, but all primes have that form
# See http://primes.utm.edu/notes/faq/six.html
# -- The algorithm works because, while it will attempt non-prime values (e.g., (6 *4) + 1 == 25),
# they will fail since the earlier repeated division (e.g., by 5) means the non-prime will fail.
# Put another way, after repeatedly dividing by a known prime, the remainder is itself a prime
# factor or a multiple of a prime factor not yet tried (e.g., greater than 5).
def factors(n)
square_root = Math.sqrt(n).ceil
factors = []
while n % 2 == 0
factors << 2
n /= 2
end
while n % 3 == 0
factors << 3
n /= 3
end
i = 6
while i < square_root
[(i - 1), (i + 1)].each do |f|
while n % f == 0
factors << f
n /= f
end
end
i += 6
end
factors << n unless n == 1
factors
end
end
end
palindrome_range.rb:
class PalindromeRange
FIXNUM_MAX = (2**(0.size * 8 -2) -1)
def initialize(min = 0, max = FIXNUM_MAX)
#min = min
#max = max
end
def downto
return enum_for(:downto) unless block_given?
n = #max
while n >= #min
yield n if is_palindrome(n)
n -= 1
end
nil
end
def each
return upto
end
def upto
return enum_for(:downto) unless block_given?
n = #min
while n <= #max
yield n if is_palindrome(n)
n += 1
end
nil
end
private
def is_palindrome(n)
s = n.to_s
i = 0
j = s.length - 1
while i <= j
break if s[i] != s[j]
i += 1
j -= 1
end
i > j
end
end
public class ProjectEuler4 {
public static void main(String[] args) {
int x = 999; // largest 3-digit number
int largestProduct = 0;
for(int y=x; y>99; y--){
int product = x*y;
if(isPalindormic(x*y)){
if(product>largestProduct){
largestProduct = product;
System.out.println("3-digit numbers product palindormic number : " + x + " * " + y + " : " + product);
}
}
if(y==100 || product < largestProduct){y=x;x--;}
}
}
public static boolean isPalindormic(int n){
int palindormic = n;
int reverse = 0;
while(n>9){
reverse = (reverse*10) + n%10;
n=n/10;
}
reverse = (reverse*10) + n;
return (reverse == palindormic);
}
}

Categories