Question

#Write a function called sum_of_primes. sum_of_primes should #take as input a single integer, and then it...

#Write a function called sum_of_primes. sum_of_primes should
#take as input a single integer, and then it should sum all
#the prime numbers up to and including that integer (if it is
#prime. Note that 1 is not considered a prime number.
#
#For example, sum_of_primes(6) would return 10: 2 + 3 + 5 = 10.
#1, 4 and 6 are not prime; 2, 3, and 5 are.
#
#Other examples include:
#
# sum_of_primes(7) -> 17 (2 + 3 + 5 + 7 = 17)
# sum_of_primes(8) -> 17 (2 + 3 + 5 + 7 = 17)
# sum_of_primes(11) -> 28 (2 + 3 + 5 + 7 + 11 = 28)
#
#To help you with this, we have supplied you with a function
#to find if a single number is prime. You do not need to know
#how this program works; only that it works. You may use this
#function, but you do not have to. Here is the function:

def is_prime(n):
from itertools import count, islice
return n > 1 and all(n % i for i in islice(count(2), int(n**0.5)-1))

#Add your function here!

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print 10, 17, 17, and 28.
print(sum_of_primes(6))
print(sum_of_primes(7))
print(sum_of_primes(8))
print(sum_of_primes(11))


Homework Answers

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or...
An integer 'n' greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Write a python program that defines a function isPrime (number) with the following header: def isPrime (number): that checks whether a number is prime or not. Use that function in your main program to count the number of prime numbers that are less than 5000....
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Write a function called "isPrime()” that checks if an integer number is prime or not. The...
Write a function called "isPrime()” that checks if an integer number is prime or not. The function returns integer 1 if the number is prime, or integer 0 if the number is not prime. Write a simple test program in main() to show that the function works properly.in C programming language.
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True...
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True if the string #has any vowels in it, False if it does not. def has_a_vowel(a_str): print("Starting...") for letter in a_str: print("Checking", letter) if letter in "aeiou": print(letter, "is a vowel, returning True") return True else: print(letter, "is not a vowel, returning False") return False print("Done!")       #Below are some lines of code that will test your function. #You can change the value of...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
using python Write a def function “amc” with a positive integer “n” input parameter. It returns...
using python Write a def function “amc” with a positive integer “n” input parameter. It returns the smallest amicable number greater than “n”. Two different numbers are both amicable if the sum of the proper divisors of each is equal to the other. Any number that's part of such a pair is an amicable number. Hint: You may want to create a separate function to sum proper divisors. def amc(n): Return the smallest amicable number greater than positive integer n....
In this problem, you will write an implementation of BubbleSort. Your function should take in a...
In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in ascending order. For example, if you receive the following input followed by a newline: 8 7 6 5 4 3 2 1 then you should display the following output followed by a newline: 1 2 3 4 5 6 7 8 Starter code for reading the input and...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT