function to get the highest common divisor between two numbers - haskell

I am trying to implement a function to get the highest common divisor between two numbers, but I am getting an error Ambiguous variable occurrence "gcd".
gcd a 0 = 0
gcd 0 a = 0
gcd a b = if a > b
then (if mod a b == 0 then a else gcd a (b-1))
else (if mod b a == 0 then b else gcd (a-1) b)
How it will be executed:
1- find the greatest of two numbers
2- then the greatest mod smallest; if it equals zero then it will return the smallest, otherwise
it will call method again but with (smallest - 1) and the same value for the greatest

Related

Sorting array of strings such that substrings of any other string will comes later

I want to find an algorithm which can sort (or arrange) array of the strings such that if any string (say B) is sub-string of any other string (say ABAC) than that B should come after ABAC.
e.g. :
suppose the string are :
abc
bc
zef
abcde
then order will be :
abcde,
abc,
bc
and zef can come anywhere in the order.
Sort algorithms are based on comparing pairs of values. Often programming languages allow to provide the built-in sort-method with a comparator function, which should take two arguments, and return an integer value indicating their relative order (-1, 0 or 1).
So define the comparator as follows:
compare(a, b):
if a is substring of b then return 1
if b is substring of a then return -1
if a < b then return -1
if a > b then return 1
return 0
This substring-test should first check the length of the two strings to potentially avoid a scan of the strings. Because when a.length > b.length, then a cannot be a substring of b. Or you could also explicitly write:
compare(a, b):
if a.length <= b.length and a is substring of b then return 1
if a.length >= b.length and b is substring of a then return -1
if a < b then return -1
if a > b then return 1
return 0
If the target programming language does not offer this possibility, then you should write your own sorting function (like QuickSort), and make sure it can use such a comparator, so that (starting from a standard implementation) you would replace:
if a < b
with:
if compare(a, b) < 0
...etc.
Transitivity of the relationship
Let's assume for a moment that the relationship that is encoded in the compare function is not transitive, so that we could find three strings a, b and c for which:
compare(a, b) < 0
compare(b, c) < 0
but also: compare(c, a) <= 0
First, note what this says about the lengths of the three strings:
compare(a, b) < 0 implies that a.length >= b.length
compare(b, c) < 0 implies that b.length >= c.length
compare(c, a) <= 0 implies that c.length >= a.length
From the first two we conclude that a.length >= c.length, and combining that with the third, we can conclude all three strings have the same length.
So now we have:
compare(a, b) < 0 implies that a is alphabetically ordered before b
compare(b, c) < 0 implies that b is alphabetically ordered before c
compare(c, a) <= 0 implies that c is alphabetically ordered before a, or is equal to a.
This leads to a contradiction. And so we must conclude that the relationship is transitive.

How to calculate the time complexity for nested for loops in the following example?

So in the following code, I am trying I am passing a (huge)number-string to the function where I have to find the maximum product of consecutive m digits
So, first, I am looping through let's say n-string and then the inner loop looping through m numbers.
So the inner loop is affected by the if-statement which makes a jump of m indexes if the next number is 0.
EDIT : 1
Actual Problem Question:
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
731671765313306249192251....(1000digits)
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Example:
m = 12 number = "1234567891120123456704832...(1000 digits)"
So in 1st iteration function will calculate the product of 1st 12 digits(i.e. from index-11 to index-0 - "1234567891120123456704832..."
Now, in 2nd iteration when it checks the value at index-12 which is 0 then index will jump to index-13. This way the loop will skip 11 iterations.
For the 3rd Iteration, the inner loop will execute for 4 iterations until it finds 0 ("0123456704832...".
def LargestProductInSeries_1(number,m):
max = -1
product = 1
index = 0
x = 0
while index < len(number)-(m-1):
for j in range(index+(m-1), index-1, -1):
num = int(number[j])
if(not num):
index = j
break
product = product * int(number[j])
max = product if max < product else max
product = 1
index += 1
return max
So according to me, the Worst Case Time Complexity would be O(n*m)
I think the Best Time would be O(n/m) if only once the inner loop is completely iterated or every mth digit is 0 which will make the outer loop execute but the index will jump to every mth digit.
Is my analysis correct?
What will be the Average Time for this case?
Will it be O(n*(log m)). Can anyone explain how? Or how to find Complexity in such cases?

Editing Excel Mod Formula

Could you please give me vba code that can solve this problem:
I want remainder in Mod Function can become equal to divisor.
Example: In normal situation Mod(132,12)=0 but I want when remainder is equal to divisor, last step of dividing that is dividing 12 on 12 doesn't do and remainder becomes 12.
Example
I wrote this code but it seems something is wrong. What's the problem?
Function XLMod(a, b)
XLMod = Int(a - (b * Int(a / b)))
If XLMod(a / 10, b) = 1 And XLMod(a, 10) = 2 Then
XLMod = b
End If
End Function
You need a special exception of the standard modulo function.
If the result of a normal division (a / b) would result in a number ending with 1 (e. g. 1, 31, 10001, 12341, ...), then you want it to return b.
Function XLMod(a, b)
XLMod = a Mod b
If XLMod = 0 And (a / b) Mod 10 = 1 Then XLMod = b
End Function

Given an non-null integer matrix, calculate the sum of it's elements

Input: First line contains T, which is the number of test cases. First line of each test case contains two integers N, M and N lines follow which contains M spaced integers.
Input:
1
2 3
1 0 0
8 -9 -1
Output:
-1
The code I've written is:
a = int(input())
for _ in range(a):
b = list(map(int,input().split()))
c = [[int(j) for j in input().split()] for i in range(len(b))]
print(sum(sum(x) if isinstance(x,list) else x for x in c))
I get the output correct. But, I'm unable to submit it because of some arising errors.

How does this Haskell function work?

I was struggling to see how this function worked. For the nth number it should calculate the sum of the previous three elements.
f' :: Integer->Integer
f' = helper 0 0 1
where
helper a b c 0 = a
helper a b c n = helper b c (a+b+c) (n-1)
Thanks for your time
Perhaps the part that you're missing is that
f' = helper 0 0 1
is the same thing as
f' x = helper 0 0 1 x
Otherwise, see Dave's answer.
It's a fairly simple recursive function. When called with three elements (I'm guessing seeds for the sequence) and a number of terms, it calls itself, cycling the seed left by one and adding the new term (a+b+c). When the "number of steps remaining" counter reaches 0, the edge case kicks in and just returns the current sequence value. This value is passed back up all the function calls, giving the final output.
The f' function provides a simple wrapper around the helper function (which does the work I described above), providing a standard seed and passing the requested term as the 4th parameter (MathematicalOrchid explains this nicely).
say its called with f' 5
below is the sequence in which it will get executed:
iteration 1: helper 0 0 1 5
iteration 2: helper 0 1 (0+0+1) 4
iteration 3: helper 1 1 (0+1+1) 3
iteration 4: helper 1 2 (1+1+2) 2
iteration 5: helper 2 4 (1+2+4) 1
iteration 6: helper 4 7 (2+4+7) 0 => 4
It is like a Fibonacci sequence, but for 3 numbers, not 2:
F'_n = F'_{n-1} + F'_{n-2} + F'_{n-3}
where Fibonacci sequence is
F_n = F_{n-1} + F_{n-2}

Resources