Popular Contest

ADVERTISEMENT

 

Codeground is hosting the World's largest coding competition. There are N participants in the contest sitting in a single row.

The energy of i’th participant from left is A[i]. To raise the popularity of the contest, some rules are added.

A participant numbered i will challenge other participant numbered j if and only if j is ahead of him (j>i) and the distance between i and j (j-i) is prime.

The contest popularity value of participant i challenging participant j is A[j]-A[i]. The total contest popularity value of the competition is sum of popularity value of all such challenges.

Given the energy of all participants, you have to find the total popularity value.

INPUT

The first line contains N, the number of participants. The next line contains N space separated integers representing the energy of all the participants.

OUTPUT

Print a single line containing the total popularity value.

CONSTRAINTS

1<=N<=1000

1 <=A[i]<= 1000000000

EXPLANATION OF SAMPLE

1 Sample input has N as 7 and participants energy as 24 6 8 10 12 14. 2. The contest popularity based on rules described is as below:

j-i                                     val diff

5-0=5                              12-2=10

3-0=3                              8-2 = 6

2-0=2                              6-2 = 4

6-1=5                              14-4 = 10

4-1=3                              10-4 = 6

3-1=2                               8-4 = 4

5-2=3                              12-6 = 6

4-2=2                              10-6 = 4

6-3=3                              14-8= 6

5-3=2                              12-8 = 4

6-4=2                              14-10=4

   Total 64

Sample Input

7 2 4 6 8 10 12 14

Sample Output

64

 

ADVERSTISEMENT

Code(python 3)

 

def solution(n,A):

    sum=0

    for i in range(n):

        for j in range(n):

            if(j>i):

                num=j-i

                if(prime(num)):

                    sum=sum+(A[j]-A[i])

    return sum

def prime(n):

    if n==1:

        return False

    for i in range(2,int(n/2)+1):

        if n%i==0:

            return False

    return True

n=int(input())

A=list(map(int,input().split()))[:n]

print(solution(n,A))