Using a different number system on conventional programming languages [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Let's say I don't need with the mainstream number system that starts at zero and increases or decreases infinitely. What I need is a cyclic number system that starts at 0 and ends at 5, pretty much like the angular system of a circle. So, if I do additions, it goes something like this:
0+1=1
1+1=2
2+1=3
3+1=4
4+1=5
5+1=0
Now, our programming languages use the traditional number system. Is there any workaround that if I type 5+1, the programming language will give me 0 every time and not the 6 symbol? No matter, if that is a Python, C, D, or other programming solution.

You are looking for arithmetic modulo 6:
for i in range(6):
print('{}+1 = {}'.format(i, (i+1)%6))
yields
0+1 = 1
1+1 = 2
2+1 = 3
3+1 = 4
4+1 = 5
5+1 = 0

Related

How to make a dynamic array Fibonacci series java program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I am in 8th grade. I am taking a 12th grade java course. I just started and my home work is to make a Fibonacci series dynamic array program. I don't know where to go. It is online so I can't ask a teacher or something.
How do you make a Fibonacci sequence using a dynamic array in Java?
This was an example of a dynamic array I got:
I don't see how you can make the Fibonacci series out of it! Here is the Fibonacci series example I got.
You can combine the two examples, as such:
Take the DynamicArrayOfInt class, and add the main method of the Fibonacci class.
Insert a new statement at the beginning of the main method instantiating a DynamicArrayOfInt object, as such:
DynamicArrayOfInt arr = new DynamicArrayOfInt();
Replace every instance of numbers[x] with arr.get(x), and instances of numbers[x] = y with arr.put(x, y).
Remove the leftover statements dealing with the numbers array. This will essentially make use of the DynamicArrayOfInt object. A sample output would look like this:
iplante$ java DynamicArrayOfInt
Size of dynamic array increased to: 2
Fibonacci series:
0
1
Size of dynamic array increased to: 4
1
2
Size of dynamic array increased to: 8
3
5
8
13
Size of dynamic array increased to: 16
21
34
55
89
144
233
377
610
Size of dynamic array increased to: 32
987
1597
2584
4181
iplante$

how to improve the efficiency? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
As a school assingment I have a really big for (it goes from 1 to a 13 digits number) with a few BigInteger operations inside. To reduce the loop I can skip all the even numbers and to avoid unnecessary BigInteger operations I can check for multiples of 3, 5, 7 and 11. Here is an exemple:
for(long i = min; i < max; i += 2){
if( i%3 != 0 && i%5 != 0 && i%7 != 0 && i%11 != 0){
BigInteger aux = new BigInteger(Long.toString(i));
BigInteger[] aux2 = k.divideAndRemainder(aux);
if(aux2[1].longValueExact() == 0){
list.add(aux);
list.add(aux2[0]);
}
}
Now, this loop would take months to finish, so I thought of breaking the for in multiple threads, each one covering a window of the original 13-digits-number.
My first question is: with a i7-3770 processor, how many threads could I have to be as fast as efficiently possible?
edit: the assingment is to otimize a problem that requires a lot of CPU. In this case, to find all divisors of a 23 digits number (the "k" in k.divideAndRemainder(aux)), thats why I'm using BigIntegers. The 13 digits number used by the for is the root of k.
Java is fine for multithreading. BigInteger is not exactly performant, but that's not the problem.
On that processor, up to 8 threads can utilize the cores completely. I doubt you run a custom OS that allows you to cede full CPU control to an application, so you want to keep at least 1 logical core for the OS and associated services, as well as 1 for Java misc threads.
If your program takes months to complete on a single thread, then using 4 threads efficiently will make it take weeks to complete. While that's a big speedup, I don't think that's quite enough for you, is it?
This is a school assignment, as you've said yourself. Just like with Project Euler, there are ways to do it, and there are ways to do it right. The first ones take days of computing on powerful machines after 10 minutes programming, the latter ones take days of thinking and seconds of computing. Your problem is not the hardware or language, it's the algorithm.
Now for the actual stuff that you can do better. From obvious to less so.
BigInteger aux = new BigInteger(Long.toString(i)); Are you mad? Why would you convert a long to a String and then parse it back to a number?! This is stupid slow. Just use BigInteger.valueOf(long val).
BigInteger is slower than primitives. Why do you need it if your values (up to 13 digits, as you've said yourself) fit comfortably in a long? If the only reason is the divideAndRemainder method - write one yourself! It's going to be 2 lines of code...
You seem to be looking for divisors of a particular number. (The filtering of multiples of 3,5,7,11 makes me think you're looking for prime ones and will do some filtering later) There is a number of speedups for such algorithms, main one being (and it's not clear whether you're using it) is making the maximum checked number the square root of the target, rather than the number itself, half of it, or whatever arbitrary bound people make up. Other ones can be found in the wiki or more complex wiki

How do i determine complexity using Big-O Notation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
What I need is to an explanation on how to determine it, here are few examples and hope you can help me find their complexity using Big-O Notation:
For each of the following, find the dominant term(s) having the sharpest increase in n and give the time complexity using Big-O notation.
Consider that we always have n>m.
Expression Dominant term(s) O(…)
5+ 0.01n^3 + 25m^3
500n +100n^1.5 + 50nlogn
0.3n+ 5n^1.5 +2.5n^1.75
n^2logn +n(log2m)^2
mlog3n +nlog2n
50n+5^3 m + 0.01n^2
It's fairly simple.
As n rises to large numbers (towards infinity), some parts of the expression becomes meaningless, so remove them.
Also, O() notation is relativistic, not absolute, meaning there is no scale, so constant factors are meaningless, so remove them.
Example: 100 + 2*n. At low numbers 100 is the main contributor to the result, but as n increases, it becomes meaningless. Since there is no scale, n and 2n is the same thing, i.e. a linear curve, so the result is O(n).
Or said another way, you choose the most extreme curve in the expression from this graph:
(source: bigocheatsheet.com)
Let's take your second example: 500n +100n^1.5 + 50nlogn
1st part is O(n).
2nd part is O(n^1.5).
3rd part is O(nlogn).
Fastest rising curve is O(n^1.5), so that is the answer.

Logic for Nine to one equals 100 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I was recently asked this question in an interview . Can anyone help me with a code to solve this problem?
We have a sequence of non-zero digits 123456789. The problem is to place plus or minus signs between them so that the result of thus described arithmetic operation will be 100. We can use a number only once . However we can use the operators(+,-,*,/) any number of times
Edit : I was asked to write a Java code for this question . So i believe its relevant
The followup question was to get all possible combinations
Example
Here is an example.
Split the numbers as follows
1 with multiply
4,7,89 with sum
3,6 with sum
4,5 with subtract
3+6 - 4 - 5 = 0
4 + 7 + 89 = 100
1 * 100 = 100
Automatic way of finding all possible combinations.
You essentially have 1 set:
the set {1..9} merged with the set {-,+,/,*,nothing} (nothing being the absence of a symbol)
You need to iterate over all the order possibilities. That will take for a long time. Exclude cases where there are 2 symbols side by side e.g. -/.
I believe this will lead to k-combinations.

return issue for method in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Need to write method calculateBoatSpeed() that takes 2 integer parameters. This first is the amount of money a team spent on their boat in dollars. The second is the number of people on the crew who own houses in New Zealand. The method should return how fast the boat will go in knots (a nautical measure of speed equal to about 1.85kph) based on the following formula: one knot for every 10 million spent plus one more for each of the crew who own a house in New Zealand.
I am new in Java, so my effort was not really succeed.
int calculateBoatSpeed(int money, int people) {
int knot= money*1000000+people;
return knot;
based on the following formula: one knot for every 10 million spent
plus one more for each of the crew who own a house in New Zealand.
What you did is multiply using *. You want to divide. Say you have:
Below 10 million, then you want 0 knots.
10 million, then you want 1 knot.
20 million, then you want 2 knots.
30 million, then you want 3 knots.
To do do this you need money divided by 10 million:
int knot = money/10000000 + people;
This is assuming that your people variable represents the number of people who own a house in New Zealand. Since we are using int data types, we shouldn't have to worrying about decimal values when dividing (using /). It'll just automatically round down to the nearest whole integer.
Think about what you've coded (suppose people=0):
if money = 1 and people=1, your formula produces 1000000 knots.
money = 2: 2000000 knots
money = 3: 3000000 knots
mighty fast boat... Think about this a bit

Categories