public static void main(String[] args) {
//Numbers
int operand1 = 25;
int operand2 = 6;
//Arithmetic values
int sum = 0;
int difference = 0;
int product = 0;
int quotient = 0;
int remainder = 0;
//Operations
sum = operand1 + operand2;
difference = operand1 - operand2;
product = operand1*operand2;
quotient = operand1/operand2;
remainder = operand1%operand2;
//Output
System.out.println("Arithmetic");
System.out.println("============================");
System.out.println("25 + 6 = " + sum);
System.out.println("25 - 6 = " + difference);
System.out.println("25 * 6 = " + product);
System.out.println("25 / 6 = " + quotient);
System.out.println("25 % 6 = " + remainder);
I'm trying to find a way to replace "25 + 6", "25 - 6", etc... with variables like
(operand1 "+" operand2 "=" sum) so the values will change dynamically depending on what value I put on oeprand1 and operand2. Is there any way to do that? Thanks
You do it the same exact way you are inserting your result variables into the strings...
System.out.println(operand1 + " + " + operand2 + " = " + sum);
You could also use printf if you wanted to although you have to insert the line break explicitly:
System.out.printf("%d + %d = %d%n", operand1, operand2, sum);
Maybe it is just me but I find this to look much cleaner.
System.out.print(MessageFormat.format(
"Arithmetic\n" +
"============================\n"+
"{0} + {1} = {2}\n"+
"{0} - {1} = {3}\n"+
"{0} * {1} = {4}\n"+
"{0} / {1} = {5}\n"+
"{0} % {1} = {6}\n", operand1,operand2,sum,difference,product,quotient,remainder));
The MessageFormat.format provides a neat alternative way to accomplish what you are looking for.
Remember sometimes less is more! Why repeat the print command over and over again?
Related
Im trying to solve EulerProblem8 https://projecteuler.net/problem=8 and i just don't get it , what am i doing wrong ? I tried before with a file and ArrayList but couldn't pull it off ... Whats wrong , the subtsrings , the loops , the *= ... i dont know what to do anymore?
package largestproductinaseries_ep8;
//The four adjacent digits in the 1000-digit number
//that have the greatest product are 9 × 9 × 8 × 9 = 5832.
//Find the thirteen adjacent digits in the 1000-digit number
//that have the greatest product. What is the value of this product?
public class LargestProductInASeries_EP8 {
public static void main(String[] args){
String bigNum = "73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
int a = 0;
String peace = "";
String onePeace = "";
int onePeaceNum = 0;
int multi = 1;
int maxMulti = 0;
while(a<bigNum.length()-12){
peace = bigNum.substring(a, a+13);
if(!peace.contains("0")){
for(int i = 12; i>=0; i--){
onePeace = peace.substring(i, i+1);
onePeaceNum = Integer.parseInt(onePeace);
multi *= onePeaceNum;
if(multi>maxMulti){
maxMulti = multi;
}
}
multi = 1;
}
a++;
}
System.out.println(maxMulti);
}
}
//23514624000 this is Euler answer
// 2091059712 this is my output
You have a problem in your implementation, you never reset the mult value so you keep multiplying and you don't stop after 13 numbers.
Your code should be:
if (mult > maxMult) {
maxMult = mult;
mult = 1;
} else {
mult = 1;
}
I need to print the factors of a perfect number. Here's the gist of my main class:
ArrayList<Integer> perfNums = new ArrayList<>();
Scanner in = new Scanner(System.in);
System.out.print("Enter the upperbound: ");
upperbound = in.nextInt();
for (int i = 1; i <= upperbound; i++) {
if (isPerfect(i)) { //boolean to check if number is a perfect number
perfNums.add(i);
}
}
System.out.println("Perfect numbers between 1 and " + upperbound + " are:");
for (int i = 0; i < perfNums.size(); i++) {
System.out.print(perfNums.get(i) + " = ");
printFactor((int)perfNums.get(i));
System.out.println();
}
Here's the printFactor class.
private static void printFactor(int number){
int factor = 1;
while(factor < number){
if (number%factor == 0) System.out.print(factor+ " + ");
//I don't know how to print the + sign otherwise.
factor++;
}
}
And here's a sample output:
Enter the upperbound: 10000
Perfect numbers between 1 and 10000 are:
6 = 1 + 2 + 3 +
28 = 1 + 2 + 4 + 7 + 14 +
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 +
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064 +
I've got the main gist of it but I've struggled with an output issue. Due to the restrictions of my online submission system, my output needs to fit exact specifications.
My question is how do I go about printing all the factors of my perfect number but removing the + sign at the end? (e.g)6 = 1 + 2 + 3
I'm not too sure of many methods to print from a while loop. Would a for-loop be better for my goals? Or are there alternative methods to print the factors of a number?
The least amount of change to address this might be something like this:
private static void printFactor(int number)
System.out.print(1);
int factor = 2;
while (factor<number) {
if (number%factor == 0) System.out.print(" + " + factor);
factor++;
}
}
1 is always a factor, so you can print that before the loop and then prepend + to every subsequent factor.
You should cache the output you want to print into a StringBuilder. Then you are able to remove the last plus sign before you print the whole String. It also has a better performance.
private static void printFactor(int number)
{
StringBuilder output = new StringBuilder();
int factor = 1;
while (factor < number)
{
if (number % factor == 0)
output.append(factor + " + ");
factor++;
}
// remove last plus sign
output.deleteCharAt(output.length() - 1);
// print the whole string
System.out.print(output.toString());
}
Since factor starts from value 1 and number % 1 == 0 will always be true, you might print 1 first and then flip factor and + in System.out.print. Like this:
private static void printFactor(int number) {
if(number > 0) {
System.out.print(1);
}
int factor = 2;
while (factor<number) {
if (number % factor == 0) {
System.out.print(" + " + factor);
}
factor++;
}
}
Not the best solution, but it will do the job.
Try to create a variable String numb and use substring method like this:
String numb ="";
while(factor<number){
if(number%factor == 0)
numb= numb + factor+ " + ";
factor++;
}
System.out.print(numb.substring(0, numb.trim().length()-1));
Just for the sake of using Java 8 :)
private static void printFactor(int number){
System.out.println(IntStream.range(1, number)
.filter(p -> number % p == 0)
.mapToObj(i -> String.valueOf(i))
.collect(Collectors.joining(" + ")));
}
Thanks everyone for the quick response. You all have been a lifesaver, and I managed to pick up some new things to consider when I code in the future.
Anyway, while waiting for a reply I was fiddling with the code and came up with a rather inelegant solution, if anybody's interested. Here's the changes to the main class:
System.out.println("Perfect numbers between 1 and " + upperbound + " are:");
for(int i=0; i<perfNums.size(); i++){
System.out.print(perfNums.get(i) + " = ");
outputString = printFactor2(perfNums.get(i));
if(outStr.endsWith(" + ")) outStr = outStr.substring(0, outStr.length()-3);
//because the submission system would cry foul with even a single extra space
System.out.println(outStr);
}
And here's the changes to the printFactor class:
private static String printFactor2(int number){
String out = "";
int factor = 1;
while(factor<number){
if(number%factor == 0) out += factor + " + ";
factor++;
}
return out;
}
Basically, what I did was append the factors to a string, then removing the trailing + sign using the substring method. On hindsight, I probably should've called the substring method inside the printFactor class instead. Something like return out.substring(0, out.length()-3); perhaps?
Nevertheless, thanks everyone!
I know my code can be simpler and more efficient... My code is supposed to grab the biggest set of 5 digits. It works, except it only is grabbing 3 digits, what would i need to modify to change that?
public class thousandDigits {
public static void main(String[] args) {
int greatest = 0;
String num = ("73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450");
for (int n = 0; n < num.length() - 5; n++) {
greatest = ((num.charAt(n)) + (num.charAt(n+1)) + (num.charAt(n+2)) + (num.charAt(n+3))
+ (num.charAt(n+4)));
if (greatest > n) {
n = greatest;
}
}
System.out.print(greatest);
}
}
OUTPUT:
357
I think you want to use String.substring(int, int) to iterate all possible 5 character substrings, and then you might use Math.max(int, int) to update greatest. Something like
int greatest = Integer.MIN_VALUE;
for (int i = 0; i < num.length() - 4; i++) {
// int value = Integer.parseInt(num.substring(i, i + 5));
int value = Integer.parseInt(String.valueOf(num.charAt(i))
+ num.charAt(1 + i) + num.charAt(2 + i) + num.charAt(3 + i)
+ num.charAt(4 + i));
greatest = Math.max(greatest, value);
}
System.out.println(greatest);
I get 99890.
I think you are trying to add 5 consecutive characters to get sum, and store starting index of highest sum.
But you should be using Character.getNumricValue(char) to convert (num.charAt(n)) to numeric value and then add.
greatest = Character.getNumericValue((num.charAt(n)) + Character.getNumericValue((num.charAt(n+1)) + Character.getNumericValue((num.charAt(n+2)) +
Character.getNumericValue((num.charAt(n+3)) +
Character.getNumericValue((num.charAt(n+4));
You need a valirable to store old value to compare and index
if(greatest > oldGreatest) {
index = n;
}
Then finally print using index out side loop:
System.out.print((num.charAt(index)) + (num.charAt(index+1) + (num.charAt(index +2)) + (num.charAt(index +3)) + (num.charAt(index +)));
Although #ElliottFrisch and #dave provides more elegant answer, I tried to modify from your original version and here is my code (I have tested it):
public class ThousandDigits {
public static void main(String[] args) {
int greatest = 0;
String num = ("73167176531330624919225119674426574742355349194934"
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450");
int max = -1;
for (int n = 0; n < num.length() - 4; n++) {
greatest = ((num.charAt(n) - '0') * 10000 + (num.charAt(n + 1) - '0') * 1000
+ (num.charAt(n + 2) - '0') * 100 + (num.charAt(n + 3) - '0') * 10 + (num.charAt(n + 4) - '0'));
if (max < greatest) {
max = greatest;
}
}
System.out.print(max);
}
}
I think you'll find it's not grabbing three digits, but rather the sum of the six characters you are pulling out is a 3-digit number.
If you're after the largest five digit number, you need to extract five digits (not six) as you do and assign them a weight. So the first digit must be multiplied by 10,000, the second by 1,000 and so on.
But there's more: you're are getting the character at an index within your string. This is not what you want as it is not the same as the numeric value of that character. For that you need:
num.charAt(n) - '0'
These changes should allow you to correct your algorithm as it stands.
A more efficient approach would be to extract 5-digit sub-strings and convert them to integers. The first one would be:
Integer.parseInt(num.subString(0, 5));
You can iterate to get each one to find the greatest.
This is what I have so far. I can't tell exactly how to change the numbers so it makes sense. Do I need to include the index as part of the equation? Although it seems like n1(the previous number) + (1/n2) should give me a new n2. Any thoughts?
package myrecursivemethod;
public class MyRecursiveMethod {
private static double index = 0;
private static double stoppingPoint=10;
public static void main(String[] args) {
double n1= 0;
double n2= 1;
System.out.println("index: " + index + "->" + n1 );
myRecursiveMethod(n1, n2);
}
public static void myRecursiveMethod(double n1, double n2)
{
System.out.println("index: " + index + " -> " + (n1+(1/n2)));
if (index == stoppingPoint)
return;
index ++;
myRecursiveMethod(n2, n1+(1/n2));
}
}
You need to take a look at your formula little closer and try to find a way to present this formula using similar formula with different arguments. For instance
sum(i) = 1 + 2 + 3 + 4 + ... + (i-1) + i
is same as
sum(i) = (1 + 2 + 3 + 4 + ... + (i-1)) + i
but since
1 + 2 + 3 + 4 + ... + (i-1) = sum(i-1)
we can rewrite entire formula as
sum(i) = sum(i-1) + i
(or actually)
{ sum(i-1) + i if i>0
sum(i) = {
{ 0 if i==0
Formula from your question is very similar to this one and can be presented in similar (recursive) way.
I am currently attempting to revise a Java Homework program for my Data Structures class that displays the output of the MaxSumTest program in a table.
I've created four arrays, and placed them inside one of the loops that calculates the timing info. I am trying to populate each array with only the timing info for one algorithm. So each array would have 4 elements. However, every time I run my revision of the program, I get an out of bounds error.
Here is the Original
The only method I revised is getTimingInfo. Here is my revision:
public static void getTimingInfo( int n, int alg )
{
int [] test = new int[ n ];
long startTime = System.currentTimeMillis( );;
long totalTime = 0;
//create an array for each Algorithm
long[] alg4;
long[] alg3;
long[] alg2;
long[] alg1;
//allocate memory for 5 long ints
alg4 = new long[5];
alg3 = new long[5];
alg2 = new long[5];
alg1 = new long[5];
int i;
int j;
int index = 0;
for( i = 0; totalTime < 4000; i++ )
{
for( j = 0; j < test.length; j++ )
test[ j ] = rand.nextInt( 100 ) - 50;
index = j;
switch( alg )
{
case 1:
maxSubSum1( test );
break;
case 2:
maxSubSum2( test );
break;
case 3:
maxSubSum3( test );
break;
case 4:
maxSubSum4( test );
break;
}
totalTime = System.currentTimeMillis( ) - startTime;
}
alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;
//Build first column of table
System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");
System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);
/*
System.out.println( "Algorithm #" + alg + "\t"
+ "N = " + test.length
+ "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
*/
}
Any advice or pointers in the right direction would be appreciated.
Shouldn't the lines that update the array elements be inside the inner loop?
alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;
You are assigning them outside the loop, where the value of index is 5, so it's out of bounds.
I'm not sure exactly what you are trying to do, but I think that what you really want to do is call getTimingInfo() a number of times and then assign the results to a table, not change the internal workings of getTimingInfo().
This requires that you define the arrays in which you will store the timing data OUTSIDE of getTimingInfo(), and then the only change inside of getTimingInfo() would be storing to that (already defined) array, rather than printing.
long[] alg1;
alg4 = new long[5];
Can be better expressed as
long[] alg1 = new long[5];
maxSubSum3( test );
Is written nicer as
maxSubSum3(test);
int j;
int index = 0;
Actually do the same thing.
Just to pick a few points.
You are setting the alg4, alg3, alg2, and alg1 to size 5, not size 4 as you said you needed.