probability of sum of a group of random numbers [closed] - probability

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 6 years ago.
Improve this question
Maybe stack overflow isn't the place for this, but it's related to a data science project I'm working on.
If I randomly generate a number between 1 and 10.... 10 times, and then total all the numbers, what will the standard deviation of that total be?
I'm pretty sure the mean of the total will be 55.5, but what about the average distance from that mean?

Well, from here one could get that mean indeed would be 10*(10+1)/2 = 55, and variance would be
10*(10-1)2/12 = 67.5.
Quick test in Python
import math
import random
def sample(a, b):
s = 0.0
for k in range (0, 10):
s += random.uniform(a, b)
return s
random.seed(12345)
a = 1.0
b = 10.0
n = 100000
q = 0.0
q2 = 0.0
for k in range(0, n):
v = sample(a, b)
q += v
q2 += v*v
q /= float(n)
q2 /= float(n)
print(q, q2 - q*q)
Prints 55.005828775627684 67.69074422910626
Std deviation would be sqrt(67.5) and equal to about 8.22

Related

Divide an integer evenly with a maximum [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 6 years ago.
Improve this question
I need an algorithm for the following:
I'm given a specified target sum n, and a specified limit m. These are both positive integers.
I want to find an integer partition of the target sum n that has as few summands as possible.
Each summand must be less than or equal to the limit m.
Within the above constraints, the summands should be as close together as possible; that is, I want n to be partitioned as evenly as possible.
So, for example, if the target sum is n = 80 and each summand must be at most m = 30, then I need at least three summands, and the most even partition is 26 + 27 + 27.
How would I compute that?
First, you get the size of the array with the following formula using integer division:
size = (variable + maximum - 1) / maximum
Next you fill the array with the following formulas:
extra = variable % size;
value = variable / size;
for each array value, set to value + 1 as long as there's extra;
value when the extra goes to zero.
Just a QnD Algorithm and Code...untested.
double n=107;
double max = 22;
int d = (int) Math.ceil(n/max);
int[] result = new int[d];
int res=0, i=0,iter=0;
while(res!=n){
iter= (int) Math.ceil(n/d);
while(iter+res>n) iter--;
res+=iter;
result[i] = iter;
System.out.println("i: " + i + " iter: " + iter + " sum: " +res);
i++;
}

How can be three 0-255 integers coded/decoded into/from one number? [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 6 years ago.
Improve this question
With three integers a,b,c ranged between 0 and 255, I have to write a function that "stores" the three given numbers into one (d) and another function that is capable to obtain from the fourth number (d), the first three numbers.
Can anyone help me with this?
You can achieve this using bitwise shift + masking. Given 4 32bits integers a, b, c and d, with a, b and c in the range [0..255]:
d = (a << 16) | (b << 8) | (c);
reverse operation:
a = (d >> 16);
b = (d >> 8) & 255;
c = (d) & 255;

Prime number optimization [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 8 years ago.
Improve this question
Below is a prime number calculator I've been working on. Currently it can calculate 100,000 prime numbers in about 22 seconds on my computer. Any thoughts on how to make the below program quicker programatically or mathematically?
include Math
print "How many prime numbers do you want?:"
x = gets.chomp.to_i #want x primes
c = 0 #want c primes
t = 3 #test number
d = 1 #divisor
n = 0 #current number of divisors
puts "2"
c+=1
while c < x
while n <= 1 && d <= Math.sqrt(t)
if(t % d == 0)
n+=1
d+=2
else
d+=2
end
end
if(n == 1)
c+=1
puts "#{t}"
end
t+=2
d = 1
n = 0
end
A fast way to find all primes up to N (or the first n primes) is to use what's called a sieve. The idea is you start with a list of integers 2,... M and associate with each either a boolean value of True, or integer 1. Then starting with 2, make every multiple of 2 (bigger than 2) false (or zero). None of these are prime because they are all divisible by 2. Then, in your list, find the next smallest value that still has True (3 in this case). Then, set all multiples of 3 (bigger than 3) to False. Then repeat. Find the next smallest number in the list that is still marked True. (4 was marked false because it was a multple of 2), 5 is the next choice. Repeat, repeat, repeat... until the smallest one you find is bigger than sqrt(M). Then every value in your list with True is a prime. This will be asymptotically much much faster than what your code does (for every value try to find a divisor).

How would I convert this code into a for loop for PYTHON [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 8 years ago.
Improve this question
B = 3
while(B <= 11):
print(B)
B = B+2
I already tried a bunch of stuff, all of it crap, like:
for B <= 11:
which is apparently invalid syntax, and i've tried:
B= 3
if B <= 11:
print(B)
B = B+2
which does absolutely nothing
So, any suggestions?
Looks like you're learning Python. What you are looking for is a range:
for B in range(3,12,2):
print(B)
Note that the parameters here are 3, 12 and 2.
The 3 is the starting point.
The 12 is used instead of 11 for the end of the range, because the range() function in Python excludes the last value of the range. You'll want to keep that in mind when writing Python code.
The 2 is the step value.
You can use something like this (Java):
for(int B = 3; B <= 11; B += 2)
{
System.out.println(B);
}

Algorithm to solve linear n equation having n 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 9 years ago.
Improve this question
I got stuck in one step while writing one algorithm. Please help me solve that.
I need to solve linear equations. Please see below image.
I used image because I don't know how to write matrix.
Please suggest me algorithm to calculate all the variables value. Looking for your kind response.
If your matrix is always of the same form given in the question (i.e. all ones, except for -1 along the diagonal), then you can solve it in time O(n) where n is the number of equations.
Let N be the number of equations.
Then the solution is given by:
t = (a+b+c)/(N-2)
x = (t-a)*0.5
y = (t-b)*0.5
z = (t-c)*0.5
Python code:
# Set up equations
a=4
b=5
c=6
A = a,b,c
# Compute inverse
t = sum(A)/(len(A)-2.)
B = [(t-x)*0.5 for x in A]
# Check
x,y,z = B
print -x+y+z
print x-y+z
print x+y-z
I derived this formula by:
Adding all the equations together to get (N-2)(x+y+z)=(a+b+c) where N is the number of equations
Writing each equation as, for example, -x+y+z = x+y+z - 2x = (a+b+c)/(N-2) - 2x = a
Solving this equation for the value of x
Looking at your examples, you look like you're trying to invert a n by n matrix of all 1's, except on the diagonal you have -1's. Let's call this matrix D_n.
Given this symmetry, the inverse is going to be similar: all a's, except on the diagonal you'll have b's (a and b to be found).
Multiplying D_n with the inverse gives us two equations that need to be satisfied (corresponding to entries in the product on the diagonal, and off the diagonal).
-b + (n-1) * a = 1
b + (n-3) * a = 0
Solving gives us:
a = 1/(2n - 4)
b = (3-n)/(2n - 4)
For example, when n=3, you have a=1/2, b=0, so the inverse, inv(D_3), is
0 0.5 0.5
0.5 0 0.5
0.5 0.5 0
Or when n=4, you have a=1/4, b=-1/4, so the inverse, inv(D_4), is
-0.2500 0.2500 0.2500 0.2500
0.2500 -0.2500 0.2500 0.2500
0.2500 0.2500 -0.2500 0.2500
0.2500 0.2500 0.2500 -0.2500
Now, you're trying to find x_1, x_2, ..., x_n (call this vector x) such that D_n * x = (a_1, a_2, ..., a_n).
Given that we've figured out how to compute inv(D_n), the solution is:
x_j = (3 - n)a_j / (2n - 4) + sum(i=1..n, i != j) (a_n / (2n - 4))

Resources