an algorithm with some arithmetics [closed] - algorithm

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 9 years ago.
Improve this question
I want to write a particular function which does some calculation but i have no idea what the algorithm should be:
Requirement
I want to write a function in which you pass in a number and it returns a value from 0 to 1.
if i pass in a 0, it returns a 1 and as you increase the value of the input the output gets closer to 0.
This is similar to a y = 1/x function
but i want to set markers such as if the input is 300, it returns a 0.75 and if it is 600 it returns a 0.5
Is there such a formula which can help me do this?

I have come up with a function which does this!
I used Lagrangian Interpolation but it's not very simple. Instead, another (far more simple) answer that works only until 600;
f(x)=-(x-1200)/1200

If you know range high value, say for example 1200
The output should be 1-(300/1200) = 1 - 0.25 = 0.75
For input x, Formula is 1 - (x/Rangehighvalue)

Related

Which order of sort and unique is faster? [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 1 year ago.
Improve this question
For example in Julia
julia> sort(unique([1,2,3,2,1]))
3-element Array{Int64,1}:
1
2
3
julia> unique(sort([1,2,3,2,1]))
3-element Array{Int64,1}:
1
2
3
Which order is faster? And why?
According to the docs, Julia uses QuickSort as default for the numeric input. This is a n log n operation.
Unique takes complextity of O(n) both time and space.
Now, consider a small input of 10000 numbers.
Case 1:
Let say only 1000 of these are unique.
Suppose you run sort(unique(arr)) then you would basically be sorting only 1000 numbers compared to unique(sort(arr)) where you would sort 10000 numbers.
Case 2:
Let say all 10000 of these are unique.
Now, here there is no significant change in the performance between the two as the sort function takes all the 10000 values either way.
So, it totally depends on input. But it makes sense to use sort(unique(arr)).

Best way to solve a first degree equation with multiple variables [closed]

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 5 years ago.
Improve this question
I would like to solve a first degree equation with multiple variables (not a system of equations) like :
10x + 5y + 7z = 630
Is there any way to solve it without using bruteforce?
Solutions must be integers.
Regroup the first two terms as 10x+5y = 5(2x+y) = 5t.
Then t/7 + z/5 = 18.
As 5 and 7 are relative primes, t = 7k and z = 5(18-k), where k is abritrary.
Finally, y = t - 2x = 7k - 2x, where x is arbitrary.
As we can check,
10 x + 5 (7k - 2x) + 7 5 (18-k) = 630.
No you can't, you have an infinity of solutions in this case.
To solve such problem you shoud have a system with at least the same number of equations as the number of variables.
Another trick, in some cases you could solve it as an underdetermined system.

Gamma distribution [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
Let X~gamma(2,1).
Find:
P(X>=2)
(X<=10)
I'm struggling to determine how to solve this. I know you must integrate some function from 2 to infinity and 0 to 10 but don't know what to integrate.
There are actually two ways to define Gamma-distribution - one with scaling 'theta' and another with inverse scale 'beta', see https://en.wikipedia.org/wiki/Gamma_distribution
Fortunately, for parameter value of 1 there is no difference.
So you have to integrate PDF
f(x) = (1/G(2)) * x * exp(-x)
Gamma function at 2 is equal to 1. G(2)=1, so
P(x) = S x exp(-x) dx
where S is integration notation.
P(x) = -(x+1)*exp(-x)
So now you have to substitute limits

How to take the output of throwing dice [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
How to display the outcome of throwing a dice with equal probability of 1,2,3,4,5,6 ?
Random no does not display with equal probability.
Thanks in advance.
If you have a reasonably good PRNG (pseudo-random number generator), you should be able to do something like (where % is the modulo operator):
print( good_prng() % 6 + 1 )
Some basic PRNGs aren't very random in the lower order bits. Without knowing what language and PRNG you're using, it's hard to say if this is the reason for what you're observing.
In Python (asked for in comment):
import random
print( random.randint(1,6) )
Python's random module uses a Mersenne twister, which is a very good PRNG.

Mathematical function where slow increase at start and fast increase at end [closed]

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 10 years ago.
Improve this question
I have number x=[0,n], where n>0.
I want to construct a function y=f(x) such that the value increase slowly from 0 and increase very fast when approaching n, and when reach n, y is infinity. What is a good function to model this?
1/(n-x) - 1/n will work.
There are plenty of other functions log, atan, x^(-k),... that goes to infinity at some point.
a^y is another set of functions with fast grows - maybe more suitable for coding as it can reach arbitrary large (but finite) values.

Resources