# 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.

9/27/2021 4:21:45 PM

Triz18 Answers

New AnswerRight 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))

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.

Here is a good tutorial on how to use nested functions: https://realpython.com/inner-functions-what-are-they-good-for/

Coding Cat , oh yeah right you are just skipping through all even numbers... It will be way faster

Hi Trizzy , this is my solution to the above question, num=int(input("Enter a number.")) print("\nThe number you entered:",num) def isPrime(num): if num>1: for i in range(2,num) : if num%i==0: return 0 return 1 else: return 0 def addPrime(i): s=0 for j in range(1,i+1): if isPrime(j): s=s+j return print("Your answer is",end=" ") print(addPrime(num)) Hope this helps.. šš»

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)

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

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))

Ok. Thank you I was having trouble defining a function in another function. Idk how to do that as yet

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)

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

Aleksei Radchenkov How can the code be written so that the isPrime function is in the addPrime function?

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))

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