Modulus operation - math

When you do division operation, the number that is being divided is called the dividend, and the number that it is dividing is called the divisor.
Question: When you do modulo operation, is there a defined naming for the two numbers involved in the operation?

I did some research on this subject. If you're asking about the following process, I'll explain.
x,y and z ∈ Z
x = y(modz)
Remainder => y
Divisor => z
"x" can be any number that gives a remainder of "y" when divided by "z".

Related

Sum, Average of elements and Count the occurence of elements in Lists in Prolog

how can I write three predicates in Prolog that do the following things:
1) Define the sum (X, N) predicate, which is true when N is the sum of integers from the list X.
2) Define the avg (X, N) predicate that calculates the arithmetic average of all elements of the list X, where N is the number of elements.
3) Define the predicate called count(X, Y, N), which is true if the list Y contains N element instances
X.
Could you give me the examples of them and explain to me why they work the way they do? I know there are dozens of sum and avg predicates here on Stack Overflow, but I can't really understand why they work.
Define the sum(X, N) predicate, which is true when N is the sum of integers from the list X.
You want to calculate the sum of the elements of a list. What would be the simplest instance of the problem? When the list is empty, the sum of its element is 0. How can we break down larger lists to get to the simple case? We can remove the first element of the list, calculate the sum of the remaining list, and then add the first element to the result.
This approach can be implemented with the following code:
% Simple case: Sum of the empty list
sum([], 0).
% Recursive case: Split list into first element X and remaining list XS
sum([X|XS], N) :- sum(XS, M), N is M + X.
Usage:
?- sum([1,2,3],6).
true.
?- sum([1,2,3],X).
X = 6.
The is operator does arithmetic evaluation of its right-hand side (reference), as opposed to treating M + X as a literal term. If you would use = instead of is, M + X would be treated as a literal term. You would then get the following output:
?- sum([1,2,3],6).
false.
?- sum([1,2,3],0+3+2+1).
true.
So for Prolog 6 and 0+3+2+1 are different terms, until you force arithmetic evaluation as done by is.
2) Define the avg (X, N) predicate that calculates the arithmetic average of all elements of the list X, where N is the number of elements.
This is not possible. If X is the list and N the number of elements, then the predicate has no way of outputting the average (unless you count printing the average as a side-effect, I don't think you want). To fix this, add another parameter A that represents the average: avg(X, N, A).
We can calculate the average by taking the sum of the list and dividing it by the length of the list:
avg(X, N, A) :- sum(X, S), length(X, N), A is S / N.
Usage:
?- avg([1,2,3],3,2).
true.
?- avg([1,2,3,4],N,X).
N = 4,
X = 2.5.
3) Define the predicate called count(X, Y, N), which is true if the list Y contains N element instances X.
I understand you want N to be the number of times that the number X occurs in the list Y. We can again break this down into a simple case and then try to break down the general case into smaller steps until we get to the simple case.
For an empty list, we know that X occurs zero times in that list. For a non-empty list, we can remove the first element and check of how often X occurs in the remaining list. If the first element if equal to X, then the total number of occurrences of X is one plus the number of occurrences in the remaining list. If the first element is not equal to X, then the total number of occurrences of X is equal to the number of occurrences in the remaining list.
This approach can be implemented with the following code:
% Simple case: The list is empty.
count(_, [], 0).
% Recursive case: First element is equal to X
count(X, [X|YS], N) :- count(X, YS, M), N is M + 1, !.
% Recursive case: First element is unequal to X
count(X, [Y|YS], N) :- X \= Y, count(X, YS, N).
We use _ for variables we do not care about. We could also write X instead of _, but Prolog would then give us a warning about an unused variable.
Usage:
?- count(1, [1,1,2,3], N).
N = 2.
?- count(2, [1,1,2,3], N).
N = 1.

how to solve ALICESIE on spoj. How it has common pattern for its answer

What is the logic behind pattern i.e.(ans=(n+1)/2) in question ALICESIE on spoj.
Algorithm_given:
1.Create a list of consecutive integers from N to 2 (N, N-1, N-2, ..., 3, 2). All of those N-1numbers are initially unmarked.
2.Initially, let P equal N, and leave this number unmarked.
3.Mark all the proper divisors of P (i.e. P remains unmarked).
4.Find the largest unmarked number from 2 to P – 1, and now let P equal this number.
5.If there were no more unmarked numbers in the list, stop. Otherwise, repeat from step 3.
Find total number of unmarked numbers.
i know its O(sqrt(n)) solution but answer is expected in O(1),it can found by seeing the common pattern i.e.(N+1)/2
But how to prove it Mathematically
link: ALICESIE

Playing with an Array [closed]

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 5 years ago.
Improve this question
Hi cant figure out solution for below problem (here is a link to the original problem: Sumita and equal array
). please help?
Sumita is playing with a Array A of size N. she wants to make all the values of A to be equal. she can multiply any number of the values in the array any number of times by X, Y and Z. Your task is to tell her whether she can do it or not. Print "She can" if she can do it else print "She can't" without ""
Input :
First line of the input will contain T (No. of test cases).
For each test case, first line will contain four space separated integers denoting N, X, Y and Z.
Then next line will contain N space separated integers of A
Output :
For every test case, print the required answer in a new line.
Constraints :
1 ≤ T ≤ 5
2 ≤ N ≤ 10^5
X, Y, Z ∈ {2, 3, 5, 7}
1 ≤ Ai ≤ 10^9
SAMPLE INPUT:
2
2 2 2 2
2 4
3 2 3 2
2 6 7
SAMPLE OUTPUT:
She can
She can't
Explanation:
Test case #1: Multiply first value by 2.
Test case #2: Not possible.
My work so far: I find out lcm of X,Y,Z if each element inside that array is divided or can divide by lcm then ans is she can else she can't
Since you give ideas of your own but no code, I will do the same: give some ideas but no code. If you try coding this idea and get stuck, edit your question by showing your code and ask for more help.
You have the right idea of finding the Least Common Multiple (LCM) of the numbers in the given array. For each element of the array, you then can calculate the quotient of the LCM and the element--this quotient is guaranteed to be a positive integer. Then see if there is any prime divisor of that quotient that is not X, Y, or Z. If there is such a divisor, then your task is not possible: "She can't". If there is no such divisor, then this element passes the test. If all elements pass this test, the task is possible: "She can".
For your first array [2, 4] and X, Y, Z values [2, 2, 2], the array's LCM is 4. The first quotient is 4 // 2 = 2, the only prime divisor of that quotient is 2, and that is in the list of X, Y, Z. The second quotient is 4 // 4 = 1, which has no prime divisors, so that element also passes the test. All elements pass the test. so the task is possible.
For your second array [2, 6, 7] and X, Y, Z values [2, 3, 2], the array's LCM is 42. The first quotient is 42 // 2 = 21, which has the prime divisors 3 and 7. The first one (3) is in your list of X, Y, Z, but the last (7) is not, so the task is impossible. There is no need to check the quotients for the other elements of the array.
Can you program that? Note that I used Python's lists rather than arrays. Also note that this algorithm depends on the possible values of X, Y, Z being prime numbers--if they could be composite numbers, the algorithm would need to be adjusted and would be more complicated. To be more precise, the algorithm would need to be changed if any two possible values of X, Y, Z were distinct and were not relatively prime (had a Greatest Common Divisor greater than one).

How to determine that remaining of dividing number X by number Y is zero using regular expression

i want to know is it possible to validate that deviding two number has remaining zero in result or not?
for example dividing number 4 on number two has zero in remaining.
4/2=0 (this is true)
but 4/3=1 (this is not true)
is there any expression for validation such case?
Better Question :
Is There any validation expression to validate this sentence ?
Remainder is zero
thank you
You can use a Modulo operator. The modulo operation finds the remainder of division of one number by another
y mod x
5 mod 2 =1 (2x2=4, 5-4=1)
9 mod 3 = 0 (3*3=9)
You can think of it, how many times does x fit in y and then take the remainder.
In computing the modulo operator is integrated in most programming languages, along with division, substraction etc. Check modulo and then your language on google (probably its mod).
This is called the modulo function. It essentially gives the remainder of a division of two integer number. So you can test for the modulo funtion returning zero. For example, in Python you would write
if a % b == 0:
# a can be divided by b with zero remainder

Find sum of factors

Why does this code return the sum of factors of a number?
In several Project Euler problems, you are asked to compute the sum of factors as a part of the problem. On one of the forums there, someone posted the following Java code as the best way of finding that sum, since you don't actually have to find the individual factors, just the prime ones (you don't need to know Java, you can skip to my summary below):
public int sumOfDivisors(int n)
{
int prod=1;
for(int k=2;k*k<=n;k++){
int p=1;
while(n%k==0){
p=p*k+1;
n/=k;
}
prod*=p;
}
if(n>1)
prod*=1+n;
return prod;
}
Now, I've tried it many times and I see that it works. The question is, why?
Say you factor 100: 1,2,4,5,10,20,25,50,100. The sum is 217. The prime factorization is 2*2*5*5. This function gives you [5*(5+1)+1]*[2*(2+1)+1] = [25+5+1]*[4+2+1] = 217
Factoring 8: 1,2,4,8. The sum is 15. The prime factorization is 2*2*2. This function gives you [2*(2*(2+1)+1)+1]=15
The algorithm boils down to (using Fi to mean the ith index of the factor F or F sub i):
return product(sum(Fi^k, k from 0 to Ni), i from 1 to m)
where m is number of unique prime factors, Ni is the number of times each unique factor occurs in the prime factorization.
Why is this formula equal to the sum of the factors? My guess is that it equals the sum of every unique combination of prime factors (i.e. every unique factor) via the distributive property, but I don't see how.
Let's look at the simplest case: when n is a power of a prime number.
The factors of k^m are 1, k, k^2, k^3 ... k^m-1.
Now let's look at the inner loop of the algorithm:
After the first iteration, we have k + 1.
After the second iteration, we have k(k+1) + 1, or k^2 + k + 1
After the third iteration, we have k^3 + k^2 + k + 1
And so on...
That's how it works for numbers that are powers of a single prime. I might sit down and generalize this to all numbers, but you might want to give it a go yourself first.
EDIT: Now that this is the accepted answer, I'll elaborate a bit more by showing how the algorithm works on numbers with two distinct prime factors. It is then straightforward to generalize that to numbers with an arbitrary amount of distinct prime factors.
The factors of x^i.y^j are x^0.y^0, x^0.y^1 ... x^0.y^j, x^1.y^0...
The inner loops for each distinct prime factor generate x^i + x^i-1 + ... + x^0 (and similarly for y). Then we just multiply them together and we have our sum of factors.
The algorithm is essentially looking at the set of all ordered subsets of the prime factors of n, which is analogous to the set of factors of n.

Resources