# Biostat823 Assignment1

***Biostat 823 assignment1-math is fun Ashley Hu***

This assignment can also be found in my blog named Biostat 823 Assignment1. 

The link for github repo is :https://github.com/AshleyHMY/BIOS823_blog 

***Q1 By listing the first six prime numbers 2,3,5,7,11 and 13, we can see that the 6th prime is 13. What is the 10001st prime number? (answered by 424,641 people)***


```python
n = 10001
prime_numbers = [2,3]
i = 3
if (0<n<3):
    print(n, 'th prime number is:', prime_numbers[n-1])
elif(n>2):
    while(True):
        i+=1
        flag = False
        for j in range(2, int(i/2)+1):
            if (i%j==0):
                # i is not a prime number if i%j==0. Only need to check the first half of i.
                flag = True
                break
        if (flag==False):
            prime_numbers.append(i)
        if(len(prime_numbers)==n):
            break
    print(n, 'th prime number is:', prime_numbers[n-1])
else:
    print('Please enter a valid number')
```

    10001 th prime number is: 104743


Explanation for Q1:
In this question I want to find the 10001st prime number let n = 10001. I created a list called prime_numbers that includes the first two prime number 2 and 3. In the first if statement when n=1 the first prime number is 2 and n=2 the second prime number is 3. I added more elements(prime numbers) to the prime_numbers list. Then I want to check if integers greater than 3 is a prime. First let i=4 and I want to check if 4 is a prime using the for loop (for j in range(2, 3). Here I used int(i/2)+1 to reduce calculation (eg, 10=2x5, if 10 can be divded by 2, we can conclude 10 is not a prime, do not need to check if 10 can be divided by 5 again). If 4 can be divided by 2 or 3, flag will be equal to True and 4 will not be added to the prime_numbers list. Then let i=5 and run the for loop (for j in range(2, 3)). 5 cannot be divided by 2 or 3, thus, 5 is a prime number and 5 is the third element in the prime_numbers list. The code will print "3th prime number is 5. let n=10001, the code will give the 10001th prime number. 

***Q2, The prime 41, can be written as the sum of six consecutive primes:
            41=2+3+5+7+11+13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to aprime, contains 21 terms, and is equal to 953. Which prime below one million can be written as the sum of the most consecutive primes?(Solved by 62,918 people)***


```python
import sympy
sum=0
num_lst = []
sum_lst = []
prime_sum_lst = []
limit = 1000000
for num in range(1,limit):
    if sympy.isprime(num) is True:
        sum+=num
        num_lst.append(num)
        sum_lst.append(sum)

for sum in sum_lst:
    if sum<limit:
        if sympy.isprime(sum)==True:
            prime_sum_lst.append(sum)


max_sum = max(prime_sum_lst)
index = sum_lst.index(max_sum)+1

print("The required answer is :", max_sum, 'contains', index, 'items')
```

    The required answer is : 958577 contains 536 items


Explanation for Q2: I use the function isprime from numpy. First, I created a list called num_lst that contains all elements below 1,000,000. I select all prime numbers from 1 to 1,000,000. The sum_lst list contains all possible consecutive sum(eg, 2,5,10,17,28,41). The second for loop select all possible sum betlow 1,000,000 that is a prime number. The prime_sum_lst includes elements (eg, 2,5,17,41). Use the mac() function to select the largest element in prime_sum_lst list. Then, use the index function to get position of max_sum in the sum_lst that is equal to the number of elements in this max_sum.

***Q3, The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way:
28 = 2^2 + 2^3 + 2^4
33 = 3^2 + 2^3 + 2^4
How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power? (solved by 20805 people)***


```python
import numpy as np

limit = 50000000

max_prime = int(limit**0.5)
#All possible prime numbers should be smaller than the square root of fifty million.

prime_sum = []

#To get a list of prime numbers below max_prime using for loop
for num in range(2, max_prime):
    if sympy.isprime(num) is True:
        num_prime.append(num)
        
for prime3 in num_prime:
    prime_forth_power = prime3**4
    if prime_forth_power > limit:
        break 
    for prime2 in num_prime:
        prime_third_forth = prime2**3 + prime_forth_power
        if prime_third_forth > limit:
            break
        for prime1 in num_prime:
            sum_power = prime1**2 + prime_third_forth
            if sum_power > limit:
                break
            prime_sum.append(sum_power)
    
def unique(list):
    """This unique function selects unique elements in the prime_sum list generated from the above for loop"""
    x = np.array(list)
    unique_lst = np.unique(x)
    return unique_lst

result = len(unique(prime_sum))
result
```




    1097343



Explanation for Q3: First use an expression num = a^2+b^2+c^2. a should be smaller than the square root of fifty million. If a^2 is greater than fifty million, this number should not be included in the expression. The max_prime is the maximum number that can be included in the expression. The num_prime includes all primes that can be used in the expression. The for loop selects numbers consists of a prime square, a prime cube and a prime fourth for prime numbers between 2 and max_prime. There are duplicate numbers in the prime_sum list. I create a function called unique() to select unique values in the prime_sum list. The "result" represents the number of unique elements int eh prime_sum list. "result" equals to the number of numbers below fifty million that can be expressed as the sum of a prime square, prime cube and prime fourth power. 

86035a27d66a71fb5d8773561df02b34b7804d98
ipynb
Jupyter Notebook
_notebooks/2021-09-03-823-Assignment1.ipynb
AshleyHMY/BIOS823_blog
ced31b0dcee7e4820852ab45077a551e65ccf433
Apache-2.0
_notebooks/2021-09-03-823-Assignment1.ipynb
AshleyHMY/BIOS823_blog
ced31b0dcee7e4820852ab45077a551e65ccf433
Apache-2.0
_notebooks/2021-09-03-823-Assignment1.ipynb
AshleyHMY/BIOS823_blog
ced31b0dcee7e4820852ab45077a551e65ccf433
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES