Prime Number | if n=4, answer=87 Calculation will be like this 2*2+3*3+5*5+7*7 if n=5, answer=208 2*2+3*3+5*5+7*7+11*11 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Prime Number | if n=4, answer=87 Calculation will be like this 2*2+3*3+5*5+7*7 if n=5, answer=208 2*2+3*3+5*5+7*7+11*11

for Number in range (1, 101): count = 0 for i in range(2, (Number//2 + 1)): if(Number % i == 0): count = count + 1 break if (count == 0 and Number != 1): print(Number) This is the prime number in range but how to implement this if n=4, answer=87 Calculation will be like this 2*2+3*3+5*5+7*7 if n=5, answer=208 2*2+3*3+5*5+7*7+11*11. Any ideas Thanks.

5th Feb 2022, 4:56 AM
Manoj Bhaagam
7 Answers
+ 3
Manoj Bhaagam May I suggest the following option. I was intrigued with the concept. from sympy import primerange as pr primes = list(pr(1,100)) #primelist n = int(input()) total =0 for i in range(n): total += primes[i]**2 print(total)
5th Feb 2022, 9:29 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
You can make a function with a parameter n, <del> use "n*2+1" as the second argument of the first range() </del> store the square of each number in a list, and return the sum. edit: wait... i did it but something still doesn't work quite right... it only works with n=4 haha 2nd edit: I made a new attempt with a "while" loop, it only works with 1 and 2, when you put 3, the loop becomes infinite wtf 😆 weird things are happening here 3rd edit: the problem was a "Tab" I deleted it and it works, so the solution is this, change the first for to a while that compares n with len() of the list, Initialize Numbers before the while, and inside the loop you add 1 to it. This added to what I already said at the beginning of this message. Anyway, thanks for the practice, I had fun for a while haha https://code.sololearn.com/ccLYDN2f785U/?ref=app
5th Feb 2022, 5:22 AM
CGM
CGM - avatar
+ 2
Per Bratthammar thank you
5th Feb 2022, 1:07 PM
Manoj Bhaagam
5th Feb 2022, 6:37 AM
Manoj Bhaagam
5th Feb 2022, 1:02 PM
Per Bratthammar
Per Bratthammar - avatar
0
Manoj Bhaagam Hi! Is it: 2**2 + 3**2 + 5**2 +… + n**2 or 2**2 + 3**3 +5**5 +… + n**n It is hard to tell from your description. However, its easy to adjust my code to get it as you want. /p 🙂
5th Feb 2022, 1:12 PM
Per Bratthammar
Per Bratthammar - avatar
0
Per Bratthammar 2nd one n*n
5th Feb 2022, 1:22 PM
Manoj Bhaagam