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
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
def largest_prime(number)
i = 2
largest_divisor = 0
while i < number
if number % i == 0
largest_divisor = i
number = number / i
i = 2
else
i += 1
end
end
number
end
The while-loop always looks for the smallest divisor of number (except 1).
If we find one, we store it, divide number and start anew.
Since we take the smallest divisor, this always stores a prime.
The loop can only end, if number == 1, which means we have divided by every factor.
And the last factor, that remained had to be the largest.
Related
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 3 months ago.
Improve this question
A MEX (Minimum Excluded) is the minimum non-negative integer that is excluded from the collection/list.
Eg :
MEX [] = 0
MEX [1,2,3,4,5,10,10000] = 0
MEX [0,1,2,3,4,5,6] = 7
MEX [0,1,3,4,1000] = 2
MEX [0,2,3,4,5,6] =1
Given a list of non negative integers, find the MEX of the list.
So, I tried sorting the array and then comparing the number at each position with its index to find the minimum number which is missing. The time complexity of this approach is O(nlogn + n). I am looking for a more optimised solution!
It can be done in linear time and linear space. You have to first realise that the result is bound by n.
So, create a lookup table (a bitset will do) with at most n+1 entries and then iterate over the input array. For each number < n flip the entry in your table to 1. The first value not 1 will by your result. By definition at least one bit has to be not 1.
Without sort-:
lis=set([0,0,2,3,1,8,4,34,12,99,100])
for i in range(0,max(lis)+2):
if i not in lis:
print(i)
break
Using Dictionary [Hashmap]-:
The (amortized) time complexity is constant (O(1)) in the size of the dictionary
lis=set([0,2,3,1,8,4,99,100])
dic={}
#for i in lis:
# dic[i]=dic.get(i,0)+1
dic=dict.fromkeys(lis, 1) # one-liner to initialize by #_aneroid
for i in range(max(lis)+1):
if i not in dic:
print(i)
break
Updated Solution:-
def solution(list_1):
if not list_1:
return 0
#Condition for all elements present
# Sum of natural numbers n*(n+1)//2 -: whole number (n-1)*n//2
n=len(list_1)-1
if sum(list_1)==(n*(n+1))//2:
return max(list_1)+1
dic=dict.fromkeys(list_1, 1)
for num in range(max(list_1)+1):
if num not in dic:
return num
break
print(solution([]))
print(solution([0,1,2,3]))
print(solution([1,2,54,32,21,46,32,0,29,88]))
print(solution(list(map(int,input().split())))) # If user want to input
Output:-
0
4
3
as user defined list..!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
How can I generate a matrix in Matlab that has 5 rows and some specific number of columns and the elements may only be binary numbers and the column sum has to be less or equal to 3?
Some possibilites without loops:
Using strings:
D = 5;
S = 3;
numbers = str2mat(dec2bin(0:2^D-1))-'0';
numbers = numbers(sum(numbers,2)<=S,:);
Using combinatorial numbers, one line:
numbers = [zeros(1,D); cell2mat(arrayfun(#(s) fliplr(full(sparse((1:nchoosek(D,s)).'*ones(1,s), nchoosek(1:D,s), 1))), 0:S, 'uni', 0).')];
How about this: The maximum binary number, that you can represent by 5bit is 2^5-1 = 31 and skip through these to find the ones with sum of digits <= 3.
Something like
n = 1:1:31;
for ii = 1:length(ii)
bin = dec2bin(ii)
digitSum = 0
for d = 1:length(bin)
digitSum = digitSum + str2num(bin(d))
end
if (digitSum <= 3)
%store results
end
end
Here is a vecotorized solution to provide all occurences efficiently:
Bstr =dec2bin(1:31);
Bstr(sum(dec2bin(0:31),2)<=sum('00111'),:)=='1'
Inspired by the solution of #pyStarter
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
I was using this formula to calculate last m digits of 2^n.
pow=2+(n-m)%(4*5^(m-1))
ans =(2^pow)%(10^m)**
But this is not working for n=2009 and m=3.
Suggest any error in my calculation or a better formula if there is.
I don't understand what your formula is doing, but the simplest way is to calculate (2^2009)%(10^m) . Here is a pseudo code to find (x^y)%mod in O(log y). Put x=2, y=2009 and mod=10^m
power(x,y)
{
if( y == 0)
return 1
temp = power(x, y/2)
if (y%2 == 0)
return (temp*temp)%mod
else
return ((x*temp%mod)*temp)%mod
}
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
i am wondering if it is possible to simplify:
T*V + V*T // V = V^(t) symmetric
where both operands are matrixes
I don't think this is possible due to the following considerations:
If we multiply two matrices A and T, where A is symmetric (i.e. A(i,j) = A(j,i)), we have the following:
For A*T we have that the item in row z and column s is computed as:
__n__
\
/ A(z,i)*T(i,s)
-----
i=1
For the other way around, T*A, we get for row z, column s:
__n__ __n__
\ \
/ T(z,i)*A(i,s) = / T(z,i)*A(s,i)
----- -----
i=1 i=1
So, as long as we do not know anything about the entries T(i,j) in T, I think we can not say how these sums relate to each other.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
Is there some way to calculate the inverse factorials of real numbers?
For example - 1.5 ! = 1.32934039
Is there some way to obtain 1.5 back if I have the value 1.32934039?
I am trying
http://www.wolframalpha.com/input/?i=Gamma^(-1)[1.32934039]
but that is a fail.
Using wolframalpha.com, you can ask for
Solve[Gamma[x+1]==1.32934039,x]
As mentioned in the comments, Gamma does not have a unique inverse. True even when you are solving for a conventional factorial, e.g.
Solve[Gamma[x+1]==6,x]
yields several answers, of which one is 3.
Instead of using Gamma[] in WolframAlpha, you can also use Factorial[]:
Solve[Factorial[x]==6,x]
Solve[Factorial[x]==1.32934039,x]
David Cantrell gives a good approximation of Γ-1(n) on this page:
k = the positive zero of the digamma function, approximately 1.461632
c = Sqrt(2*pi)/e - Γ(k), approximately 0.036534
L(x) = ln((x+c)/Sqrt(2*pi))
W(x) = Lambert W function
ApproxInvGamma(x) = L(x) / W(L(x) / e) + 1/2
For integers you can do:
i = 2
n = someNum
while (n != 1):
n /= i
i += 1
return (i==1 ? i : None)
The factorial for real numbers has no inverse. You say that "each function must have an inverse". That is incorrect. Consider the constant function f(x)=0. What is f^-1(42)? For a function to be inverse it must be both an injection and a surjection.