I need to solve this equation in my Java app:
(1080 * j + 1) modulo 7 = 0
Is there some more safe way how to get this value instead of this code? I am not much happy with while loop condition.
int j = 1;
int e = 7;
boolean found = false;
double r = 0;
while (!found) {
r = (1080 * j + 1);
found = r % e == 0;
j++;
}
int t = (int) (r / e);
You can improve your solution significantly using maths. You need to find a number that multiplied by 1080 will given a remainder 6 modulo 7 (because after adding 1 it should be divisible by 7). Now 1080 gives remainder 2 modulo 7. Thus you need to find number that multiplied by 2 gives 6 modulo 7. Lets check all 7 possible remainders:
0 * 2 = 0 (modulo 7)
1 * 2 = 2 (modulo 7)
2 * 2 = 4 (modulo 7)
3 * 2 = 6 (modulo 7)
4 * 2 = 1 (modulo 7)
5 * 2 = 3 (modulo 7)
6 * 2 = 5 (modulo 7)
So the only solutions to your problem are the numbers giving remainder 3 (modulo 7) and all such numbers are solutions of the equation.
(1080*j + 1)% 7 =((1080*j)%7 + 1%7 )%7
And (1080*j)%7 = ((1080%7)*(j%7))%7 = (2*(j%7))% 7
And actually, j only need to run from 0 to 6, as you can clearly see that this is a periodic cycle, which help you to avoid infinite loop, as well as any number (not necessary 7)
(1080*j+1) mod 7 = 0 => 1080*j = 1 (mod 7). So you can use For loop from 0 to 6 like this :
int j;
for(int i=0; i<7; i++){
if((1080*i) % 7==1) {
j=i; break;
}
}
Here is a way to simplify the equation :-
(1080*j + 1)mod 7 = 0
(1080*j)mod7 = 6
by multiplication theorem of modular arithmetic : -
(a*b)%k = (a%k * b%k)%k hence (1080*j)%7 = (1080%7 * j%7)
1080%7 = 2
hence (1080*j)%7 = (2* j%7)%7 = 6
Now j%7 could have values (0,1,2,3,4,5,6)
now of all possible values j%7 = 3 would give (2*3)%7 = 6
j%7 = 3
therefore j = k*7 + 3 is solution to equation where k is any whole
number
Related
Given a decimal number represented as list from least significant bit to most significant bit how should one find mod with k if the list were a number.
Example :- number - 1234567
list representation of number: 7->6->5->4->3->2->1
k = 7
ans = 1234567%7 = 5
In python and java I could have converted the list to a number BigInteger(java) and have taken the mod. But I wanted to do it without using BigInteger.
I have tried calculating the mod while iterating the list
total=0
for digit in numberList:
total = (total * 10 + digit) % k
return total
But this is not going to work.
You can calculate the remainder of the i'th digit multiplied by 10^i for each digit. Then sum all these remainders and find the remainder of the sum :
int mul = 1;
int remainder = 0;
for (int digit : digitArray)
{
remainder += (digit * mul) % k;
mul *= 10;
}
remainder = remainder % k;
For 1234567%7, this loop basically computes :
(7 % 7 + 60 % 7 + 500 % 7 + 4000 % 7 + 30000 % 7 + 200000 % 7 + 1000000 % 7) % 7 =
(0 + 4 + 3 + 3 + 5 + 3 + 1) % 7 = 19 % 7 = 5
Your code is close, but you need to process your number list in reverse. I'll illustrate with a simple Python list.
num = 1234567
# Construct numberList
numberList = []
n = num
while n:
n, d = divmod(n, 10)
numberList.append(d)
k = 7
print(num, numberList, num % k)
# Find numberList mod k
r = 0
for d in reversed(numberList):
r = (10 * r + d) % k
print(r)
output
1234567 [7, 6, 5, 4, 3, 2, 1] 5
5
Think of the basic property of modular arithmetic :
(a*b)%m=(a%m * b%m ) % m
(a+b)%m=(a%m + b%m ) % m
Now, to find modulo of 1234567
1234567 % m = (1234 * 1000 + 567)%m
=((1234 * 1000)%m + 567%m) %m
Now, take a little small no. to better understand this trick
12345 =( ( (1 * 10 + 2) * 10 + 3) * 10 + 4) *10 +5
Now,
12345 % m = (((((((1*10)%m + 2)*10)%m + 3)*10)%m + 4)*10 + 5)%m
Now to find modulo of a number that is represented by a linked list its actually easy
list1: 1->2->3->4->5.......
take variable result and initialize with 0
i.e., result=0 and just traverse the list and do:
result = (result*10 )% m + node->data
see the code below it is too simple
int result=0;
while(list1){
int x=list1->data;
result=(result*10)%m + x;
list1=list1->next;
}
result=result%m;
return result;
This question already has answers here:
How does this while block work? [closed]
(2 answers)
Closed 7 years ago.
Consider the following method:
public int foo(int n) {
int x = 1;
int k = 0;
while (x <= n) {
x = x * 2;
k = k + 1;
}
return k;
}
What value is returned by foo(13)? I know the answer is 4 but could someone please tell me why it is 4?
x doubles with every iteration through the loop, and k increases by 1 every time through.
It's simple enough to draw out with a table.
x | k
1 | 0
2 | 1
4 | 2
8 | 3
16 | 4
32 | <end of loop>
x doubles at each step until it becomes greater than 13. So x = 1 -> 2 -> 4 -> 8 -> 16. So it gets doubled 4 times and k is also incremented 4 times. So from 0 it becomes 4.
Below is the step/algorithm steps:
X=1 k=0 n =13
Step 1: x=2 k=1
Step 2: x=4 k=2
Step 3: x=8 k = 3. Since 8<13...
Step 4: x=16 k= 4. 16>13, so return k=4.
Its finding n < 2^k where k is your answer. When n = 13: 13 < 2^k = 2^4 = 16.
K is the number of times the while loop is entered.
As x is 2 power, 2^3 is little than 13, so it enters one last time, 2^4 is bigger than 13, and then k is 4
pretty simple...
public int foo([13]) {
int x = 1;
int k = 0;
while (x <= n) {
x = x * 2;
k = k + 1;
}
return k;
}
your while loop stops when x is bigger or equal to n[13]
each time x is multiplied by 2
(definition)x = 1
x = 2
x = 4
x = 8
x = 16 (is now over n[13])
and so the while loop runs 4 times
while (x <= n) {
x = x * 2;
k = k + 1;//dis thang
}
k[0]+1=1
k[1]+1=2
k[2]+1=3
k[3]+1=4
and thats why its 4.
Hello i am having a tough time trying to write a function that can create an array that holds integers, that equal my simple math problem.
my problem is not adding integers into the array but finding the correct integers that could add up to a math problem.
for example i have a simple math problem like: 10 + 10 = ? we know it equals 20
so i want my array to hold up to ten integers, that when added all together equal 20.
this is what i have been trying in code but not getting the results i want.
while (totalCount != answer
&& count < setCount) {
randomNumber = rand.nextInt((int) answer / 2) + 1;
if(count < setCount) {
sumOfBalloons.add(randomNumber);
totalCount += randomNumber;
count++;
}
if(totalCount > answer) {
count = 0;
totalCount = 0;
sumOfBalloons.clear();
}
}
i am trying to find random numbers that add up to the math problems answer so i can draw them on balloons. problem is i can never get ten numbers to equal the answer in my while loop.
does anyone know some way of doing something like this?
need array to hold 3 - 10 integers that equals my math problems answer.
** update on code thanks to the advice i received i managed to fix my while loop now it looks like this
had to post like this cause my rep is very low. sorry.
while (totalCount != answer) {
randomNumber = rand.nextInt((int) answer / 2) + 1;
if(totalCount + randomNumber > answer) {
randomNumber = rand.nextInt((int) answer - totalCount) + 1;
}
if(count + 1 == setCount) {
randomNumber = answer - totalCount;
}
if(count < setCount) {
sumOfBalloons.add(randomNumber);
totalCount += randomNumber;
count++;
}
if(totalCount > answer
|| totalCount == answer
&& count < setCount
|| totalCount != answer
&& count == setCount) {
count = 0;
totalCount = 0;
sumOfBalloons.clear();
}
}
this is what i got in my console from this code
Total count = 10
Total totalCount = 20
sumOfBalloons 0 = 2
sumOfBalloons 1 = 3
sumOfBalloons 2 = 3
sumOfBalloons 3 = 2
sumOfBalloons 4 = 1
sumOfBalloons 5 = 4
sumOfBalloons 6 = 2
sumOfBalloons 7 = 1
sumOfBalloons 8 = 1
sumOfBalloons 9 = 1
I think there are a few options here re: generating random numbers that sum to 20.
Here's one possible solution:
Create an array of length 4, for example.
Generate random number between 1 and 6 for each of the first 3 indices of your array.
At this point you'll have an array of the form: { 4, 5, 2, _ } (where our 4th element hasn't been chosen yet).
Sum our first 3 elements: 4 + 5 + 2 = 11. Determine 4th element by calculating 20 - current_total (11) = 9.
Set myArray[3] = 9;
A few things to note:
You may need to modify the range of possible random numbers ( 1-6 ) I've given. Consider what happens if the array we generate turns out to be { 2, 1, 2, _ }...then there's no digit that will ensure the elements sum to 20.
Another option is to use an arrayList instead of an array. The benefit to this is that you can keep adding elements to your arrayList until you either hit 20 (then you're done) or go over (in which case you delete the most recent element and begin adding again). You also won't need (or be able) to know the length of your arrayList in advance.
I have this poker card game where a possible 13 card ranks stored as [0 to 12].
Each hand which holds 5 cards which have 13 possible card ranks.
The final value is a identifier which begins from the exponent 13⁵ (to power of 5).
Which stores what kind of winning hand it is.
Then the remaining 5 powers of 13 are used to store each of the 5 cards.
Also not to mention not all 5 cards are stored at all times, that's only for the High Card win, which requires 4 kickers.
My question is using just the final value how would I be able to unpack every card and what kind of winning hand it was.
/** The ranking factors (powers of 13, the number of ranks). */
private static final int[] RANKING_FACTORS = {371293, 28561, 2197, 169, 13, 1};
rankings[0] = HIGHCARD WIN [0]
rankings[1] = 12; //Ace
rankings[2] = 6; //Eight
rankings[3] = 9; //Jack
rankings[4] = 1; //Three
rankings[5] = 3; //Five
// Calculate value.
for (int i = 0; i < NO_OF_RANKINGS; i++) {
value += rankings[i] * RANKING_FACTORS[i];
}
(0*371293) + (12*28561) + (6*2197) + (9*169) + (1*13) + (3*1) = 357451
Attempting to unpack the values from that 357451 value.
Starting trying to figure out the math here.
if 357451 < 371293 rankings[0] = 0
(357451 / 28561) = 12 rankings[1] = 12
(357451 / 2197) / ((13*2)+1) = 6 rankings[2] = 6
(357451 / 169) / ((13*18)+1) = 9 rankings[3] = 9
//Alright it seems that 18 is from answers (12+6) probably because I haven't subtracted them or something.
//So next one should be (12+6+9)= 27, but it's 2115
(357451 / 13) / ((13*2115)+1) = 1 rankings[4] = 1
(357451 / 1) / ((13*9165)+1) = 3 rankings[5] = 3
I think I figured it out but I don't understand the values Probably also only works for this case will break on any other case.
Don't know where the values 2, 18, 2115, 9165 get generated from probably some crap I made up.
How do I do this the proper way? I don't think I could use shifting since this isn't bitwise.
So its done like this then?
(357451 / 371293) = 0
(357451 / 28561) = 12
(357451 % 28561) / 2197 = 6
(357451 % 2197) / 169 = 9
(357451 % 169) / 13 = 1
(357451 % 13) = 3
You're correct through this part..
(357451 / 28561) = 12 rankings[1] = 12
But this is no good...
(357451 / 2197) / ((13*2)+1) = 6 rankings[2] = 6
You need to take your result 12 and multiply it back to 28561 then subtract that from 357451 to see what was left over. In this case it's 14719.
Now you can continue using that number instead of 357451. So 14719 / 2197 = 6.
Continue that pattern (14719 - (2197*6)) until you have your 5 numbers.
(357451 % 28561) will also get you the remainder, if you want to do it that way.
My "decode" code...
private static final int[] RANKING_FACTORS = {4826809, 371293, 28561, 2197, 169, 13, 1};
#Test
public void testDecode() {
long value = 357451;
int[] rankings = new int[6];
//System.out.println(Math.max(0,value-RANKING_FACTORS[0]));
for (int i=0; i < rankings.length; i++) {
rankings[i] = (int)(value / RANKING_FACTORS[i]);
value %= RANKING_FACTORS[i];
System.out.println(rankings[i]);
}
}
The example in the book asks the user to enter any positive number. Then the program will add the individual digits separately and print the total. For example if the user enters the number 7512 the program is designed to add 7 + 5 + 1 + 2 and then print the total.
I've written out the way I understand how the code works. Is this correct? Is my understanding of this loop correct with each step, or am I missing any calculations? What happens during the 4th loop when there is no remainder in 7 % 10?
1st run of loop ... sum = sum + 7512 % 10 which is equal to 2
n = 7512 / 10 which which equals to 751
2nd run of loop ... sum = 2 + 751 % 10 which is equal to 1
n = 751 / 10 which is equal to 75
3rd run of loop ... sum = 3 + 75 % 10 which is equal to 5
n = 75 / 10 which is equal to 7
4th run of loop ... sum = 8 + 7 % 10 <------?
import acm.program.*;
public class DigitSum extends ConsoleProgram{
public void run() {
println("This program will add the integers in the number you enter.");
int n = readInt("Enter a positive integer: ");
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
println("The sum of the digits is" + sum + ".");
}
}
The result of the operation 7 % 10 is 7, the remainder when you divide 7 by 10. The last iteration of the loop is to add 7 to the prior value. The next division step inside the loop (n /= 10;) takes n to 0, which is what ends the loop.
% is not the same as /
The % operator is for the modulus, not division... This means that the result of the operations is not dividing, but obtaining the remainder of the division, like:
7512 % 10 => 2
751 % 10 => 1
75 % 10 => 5
7 % 10 => 7
This kind of logic is fairly frequently used when dealing with numeric operations.
before run, sum = 0, n = 7512
1st run of loop ... sum = 0 + 2 => sum = 2, n = 751
2nd run of loop ... sum = 2 + 1 => sum = 3, n = 75
3rd run of loop ... sum = 3 + 5 => sum = 8, n = 7
4th run of loop ... sum = 8 + 7 => sum = 15, n = 0
After 7%10 you get 7 and that is added to your result.
And 7/10 will result in 0 and hence your loop ends and your sum now has addition that you want.