Modular Arithmetic in programming [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.

The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.

Related

Take a result of two long numbers using an array in Java [closed]

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 want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm

Why is BigDecimal's scale not a BigInteger? [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 7 years ago.
Improve this question
Why is the scale field of a BigDecimal not a BigInteger? I assume currently it makes maybe no sense because calculations with that many decimal places are likely never performed, but wouldn't it make sense for the future to rather use a BigInteger?
The scale of a BigDecimal is the number of digits it stores to the right of the decimal point. It is an amount of memory in that those digits are actually stored, and it is an amount of work in that most operations on a BigDecimal will have to do work on all of those digits.
It is never going to be a good idea to use a BigDecimal that takes an amount of memory or work that doesn't fit into an int, so an int is used for scale. That's the same reason int is used for string lengths and collection sizes, etc.
In the rare cases that a reasonable amount of memory or work doesn't fit into an int, it certainly fits into a long. Longs are used for file sizes and position, for example. A BigInteger is never required.

Could someone help me understand what this is asking [closed]

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 8 years ago.
Improve this question
To help Beta Rabbit crack the lock, write a function answer(n) which returns the smallest positive integer base b, at least 2, in which the integer n is a palindrome. The input n will satisfy "0 <= n <= 1000."
Test cases
Inputs:
(int) n = 0
Output:
(int) 2
Inputs:
(int) n = 42
Output:
(int) 4
it is not the problem I need help with as there are similar ones on this site. It is the actual question. this input n they talk about, what the hell does it stand for? It can't be the base as b represents the base. It is not the positive integer they want for output because it is independent of that number and in the test cases it doesn't seem to have any correlation with it. Does it just want me to find the smallest palindrome of any base that's less than 1000 in decimal? Thanks to anyone that takes the time to help me figure this out , it's part of Google's foobar questions. I don't mind doing the work to solve the flecking thing as long as I can understand what the thing is asking.
What is Google Foobar?
I may not give you a full answer, but think about it this way. The number n, convert it to the output base... see if it's a palindrome

How to get integer of a value? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How to round up a decimal number to a whole number?
So for example when it is 1.25 rounds to 1 or -3.25 rounds to -3 etc
if you want decimal part do this .
double value = 1.25;
int i = (int)value;
if you want to round value , do this
Math.round(value);
Use Math.round(float number) method http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round(float) if you want to round it. If you want to cut decimal part just cast it to int.
A small trick I've learned during the years is just adding the value 0.5 to the value you want to round. After you did that, you can round it to an integer.
It's a generic workaround, which will work with alot of programming languages.
I dont know much about Java, but I think you will find a round-Method in the API. The package should be Math.
Edit:\
To round UP you could just add 1 instead of the value 0.5.

Find the reverse of a 4 digit integer [closed]

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 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321

Categories

Resources