How do I solve this using Python? Iā€™m struggling šŸ˜­ | Sololearn: Learn to code for FREE!

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

9/27/2021 4:21:45 PM

Triz

18 Answers

New Answer

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

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

+2

It works now. Thank you very much !

+1

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

+1

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

+1

Olivia , khm.... The same way as you remember how to walk)

+1

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.. šŸ‘šŸ»

+1

Check this code https://code.sololearn.com/cJxJf9Ki5652/?ref=app

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)

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

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

0

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

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)

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

0

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

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

0

Ooh ok. Nice

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