Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Trying to figure out if I wanted to run a number 17 digits long through 50 million divisions how long would it take on a decent i7 PC and what would you recommend for language? Also I would like to scale it up over time so need a language that can be flexible when I get to say 30ish digit long numbers. For now basically I begin with a 17 digit long number and as I go I only care about the a smaller number after each calculation so it will get smaller quick. I am only doing division and subtraction and not keeping any remainders. Thoughts?
I've knocked up a quick program to test how long it takes, here I believe I have used a 20 digit long number. I know it's not exactly the parameters that you have asked for, however it gives a good illustration to what kind of speed you can be expecting.
This was run on a i5-6200U # 2.30GHz
If you are interested on the code that I used here it is. It will not be able to divide 30 digits but a little tweaking will allow it to. This is written in C#.
using System;
namespace _50_Million
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter Number ");
string number = Console.ReadLine();
Console.Write("What to divide by ");
string divide = Console.ReadLine();
Console.Write("How many times ");
string d = Console.ReadLine();
decimal previousNumber = Convert.ToDecimal(number);
decimal times = Convert.ToDecimal(d);
decimal divideDecimal = Convert.ToDecimal(divide);
var watch = System.Diagnostics.Stopwatch.StartNew();
decimal newNumber = 0;
for (int i = 0; i < times; i++)
{
newNumber = previousNumber / divideDecimal;
previousNumber = newNumber;
}
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("It has taken " + elapsedMs + " millisecounds to divide " + number + " by " + divide + ", " + d + " times.");
Console.WriteLine("The Answer is " + newNumber);
Console.ReadLine();
}
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a small problem. I think it's simple, but I don't know, how to manage it properly.
I have this simple int:
int birth = 011112;
and I want output to look like this, in this specific format.
"Your birth date is 12.11.01."
I did it with an integer array, but I want only one integer input like this one, not an array.
Could some body help me? Is there any simple method to manage it of, without using loops?
Basically, the conversion of the int representing a date in some format into String should use divide / and modulo % operations, conversion to String may use String.format to provide leading 0.
The number starting with 0 is written in octal notation, where the digits in range 0-7 are used, so the literals like 010819 or 080928 cannot even be written in Java code as int because of the compilation error:
error: integer number too large
int birth = 010819;
However, (only for the purpose of this exercise) we may assume that the acceptable octal numbers start with 01 or 02 then such numbers are below 10000 decimal.
Then the numeric base for division/modulo and the type of output (%d for decimal or %o for octal) can be defined:
public static String rotate(int x) {
int base = x < 10000 ? 0100 : 100;
String type = x < 10000 ? "o" : "d";
int[] d = {
x % base,
x / base % base,
x / (base * base)
};
return String.format("%02" + type + ".%02" + type + ".%02" + type, d[0], d[1], d[2]);
}
Tests:
System.out.println(rotate(011112)); // octal
System.out.println(rotate(11112)); // decimal (no leading 0)
Output:
12.11.01
12.11.01
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I just want to convert seconds to proper time format.
e.g. if it is 30 seconds, then it is just displayed as 30 seconds.
if it is 80 seconds, it will be displayed as 1 minute 20 seconds
It is similar when it is larger than one hour and one day.
Of course it is simple to implement by myself, just just wonder whether there's any existed library I can leverage. Thanks
TimeUnit belongs to the package java.util.concurrent. TimeUnit has come in java since JDK 1.5. TimeUnit plays with a unit of time. TimeUnit has many units like DAYS, MINUTES, SECONDS, etc.
But it doesn't directly convert seconds to min, days, hours... So, you can refer my code below to convert seconds to minutes, days, and hours.
import java.util.concurrent.TimeUnit;
import java.util.Scanner;
public class Time {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int seconds = keyboard.nextInt();
int day = (int) TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) -
TimeUnit.DAYS.toHours(day);
long minute = TimeUnit.SECONDS.toMinutes(seconds) -
TimeUnit.DAYS.toMinutes(day) -
TimeUnit.HOURS.toMinutes(hours);
long second = TimeUnit.SECONDS.toSeconds(seconds) -
TimeUnit.DAYS.toSeconds(day) -
TimeUnit.HOURS.toSeconds(hours) -
TimeUnit.MINUTES.toSeconds(minute);
System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
}}
`
Hope this code help you!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have expression like below, and I need to parse them.
Examples:
Total volumes of A + Total volumes of B
Total sales of A / Total sales of B
Total Shipment of A - Total Shipment of B
When I get the string , I don't know which operation is present in the string. So using regex I want to parse each expression in java, and I should get this result.
expression1 = Total volumes of A
expression2 = Total volumes of B
operator = +
Can anyone help me with this. I want an efficient way to do this in java, there are 1000's of such expression
As #Pshemo stated, groups are the solution.
I did make the assumption that operator characters aren't part of the operands:
Pattern pattern = Pattern.compile("([\\w\\s]+)\\s+([+\\-/*])\\s+([\\w\\s]+)");
Matcher matcher = pattern.matcher("Total volumes of A + Total volumes of B");
if (matcher.matches()) {
System.out.println("operand 1: " + matcher.group(1));
System.out.println("operator: " + matcher.group(2));
System.out.println("operand 2: " + matcher.group(3));
}
Output:
operand 1: Total volumes of A
operator: +
operand 2: Total volumes of B
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm trying to write a simple program from this task.
The number of calories burned per hours by cycling , jogging, and swimming are 200, 475, and 275 respectively. A person loses 1 pound of weight for each 3500 calories burned. Write a program that declares 3 variables, one to store the number of hours spent jogging, the second to store the number of hours spent cycling and the third to store the number of hours spent swimming. Assign each of these variables values. Calculate and display the number of pounds worked off.
The code I have written is:
public class task2
{
public static void main(String[] args)
{
double c = 2; //2 hrs of cycling
double j = 1; //1 hr of jogging
double s = 2; //2 hrs of swimming
double cycle = c * 200; //400 calories
double jog = j * 475; //475 calories
double swim = s * 275; //550 calories
double sum = cycle + jog + swim / 3500; //1425 / 3500 is what should be in here
System.out.println("You've burned " + sum + " calories");
}
}
The answer I get back is:
"You've burned 0.40714286 calories"
but I get back:
"You've burned 875.15871428571428 calories".
I don't know where I went wrong. I want the output to be a double so it can show the answer if it's below 3500 calories.
You might meant: double sum = (cycle + jog + swim) / 3500;
You need () to group all the +'s so that the addition is carried out before the division:
double sum = (cycle + jog + swim) / 3500;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm still learning Java....
My task is to write a program which divides two doubles, but before it displays result of dividing, it has to say if they are dividible or not (without a rest). I HAVE TO use ternary operator.
code:
public class exercise {
public static void main(String[] args){
double x = 8.4;
double y = 4.2;
double z = (int)(x % y);
String result = (z>0) ? "not dividible" : "dividible";
System.out.println("This operation is: " + result);
System.out.println("The result of dividing is: " + (x/y));
}
}
Compiler says for the line "String result = (z>0) ?....." that it requires boolean, and that it found int. Of course, compilation failed.
You've missed the last closing bracket ()) of this statement.
System.out.println("The result of dividing is: " + (x/y);
It should be:
System.out.println("The result of dividing is: " + (x/y));
↑
All the other stuff compiles for me.
My compiler says that you dont have last parenthesis ) in this line:
System.out.println("The result of dividing is: " + (x/y);
Try this:
System.out.println("The result of dividing is: " + (x/y));
It compiles for me.
The problem comes from the syntax error, due you have forgotten the last closing bracket () of the last system.out line.
System.out.println("The result of dividing is: " + (x/y);
Should be:
System.out.println("The result of dividing is: " + (x/y));
If you solve this, then there is no problem.