How do I solve this using Python? I’m struggling 😭 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do I solve this using Python? I’m struggling 😭

A function isPrime () is given to you that given as an integer as an argument returns True if it is prime and False otherwise. def isPrime (number) : if number > 1: for x in range (2, number) : if number % X == 0: return False return True else: return False Copy this code in ur code file. Write a function addPrimes that sums all prime numbers between 1 and x (where, x > 1). You need to use this function isPrime to check if a given number is prime.

27th Sep 2021, 4:21 PM
Triz
Triz - avatar
17 Answers
+ 2
Right that is because it is running through range (1, X) when it shall be (1, X+1) Try: def isPrime (number) : if number > 1: for x in range (2, number) : if number % x == 0: return False return True else: return False def addPrime(x): sum = 0 for i in range(1, x+1): if isPrime(i): sum+=i; return sum print(addPrime(5))
27th Sep 2021, 5:13 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Alright here is an addPrime function that uses isPrime: def addPrime(x): sum = 0 for i in range(1, x): if isPrime(i): sum+=i; return sum However I'm almost certain your isPrime function is incorrect.
27th Sep 2021, 4:39 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
It works now. Thank you very much !
27th Sep 2021, 5:19 PM
Triz
Triz - avatar
+ 1
Here is a good tutorial on how to use nested functions: https://realpython.com/inner-functions-what-are-they-good-for/
27th Sep 2021, 4:58 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Coding Cat , oh yeah right you are just skipping through all even numbers... It will be way faster
27th Sep 2021, 10:19 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Olivia , khm.... The same way as you remember how to walk)
29th Sep 2021, 1:12 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
29th Sep 2021, 2:41 PM
CHITTAMURU NIKHIL
0
Your attempt? it's really not that hard you just make a loop that goes through every number from 1 to X and check on whether it is prime using isPrime() function. (if it is prime add it to the sum)
27th Sep 2021, 4:22 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
Aleksei Radchenkov I’m new to programming. This is what i did: def isPrime(number): x= isPrime(number) if number > 1 : for x in range(2,number): if number % x == 0: return False return True else: return False if x is True: total= 0 addPrimes= total + x return addPrimes
27th Sep 2021, 4:31 PM
Triz
Triz - avatar
0
Actually your isPrime function will work but it is far not the most efficient approach: Here is a better code: from math import sqrt def isPrime (n) : prime_flag = 0 if(n > 1): for i in range(2, int(sqrt(n)) + 1): if (n % i == 0): prime_flag = 1 break if prime_flag == 0: return True else: return False else: return False def addPrime(x): sum = 0 for i in range(1, x): if isPrime(i): sum+=i; return sum print(addPrime(7))
27th Sep 2021, 4:48 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
Ok. Thank you I was having trouble defining a function in another function. Idk how to do that as yet
27th Sep 2021, 4:56 PM
Triz
Triz - avatar
0
I run the code against some test cases and i think something is missing from the code. So these are the answers that I’m supposed to get when call addPrime: >>>addPrime(5) 10 >>>addPrime(200) 4227 >>>addPrime(3) 5 I only got the correct ans for addPrime(200)
27th Sep 2021, 5:07 PM
Triz
Triz - avatar
0
Aleksei Radchenkov maybe you are interested in this: It works w/o a flag and needs only the half of steps in the for loop. def isPrime (n) : if(n <= 1): return False if (n == 2): return True if (n%2 == 0): return False for i in range(3, int(sqrt(n)),2): if (n % i == 0): return False return True
27th Sep 2021, 5:59 PM
Coding Cat
Coding Cat - avatar
0
Aleksei Radchenkov How can the code be written so that the isPrime function is in the addPrime function?
27th Sep 2021, 10:04 PM
Triz
Triz - avatar
0
Trizzy , something like: def addPrime(x): def isPrime (number) : if number > 1: for x in range (2, number) : if number % x == 0: return False return True else: return False sum = 0 for i in range(1, x+1): if isPrime(i): sum+=i; return sum print(addPrime(5))
27th Sep 2021, 10:22 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
Ooh ok. Nice
27th Sep 2021, 11:04 PM
Triz
Triz - avatar
0
You should use range(2,int(number**0.5)+1) instead using range(2,number) Because if that num is not prime , there are probably 1 or more pair, each pair has a num <= square root of num and a num >= square root of num Ex : num is 10 10 % 2 == 0 2 < 10 ** 0.5 => It's not prime
29th Sep 2021, 12:48 PM
TCorn
TCorn - avatar