Write a function composite2(N) which takes an integer N as an input and returns the
Nth composite number which is NOT divisible by 2. For instance, composite2(3) returns
21 while composite2(5) returns 27.
"Rishee Barthakur" wrote:
> Write a function composite2(N) which takes an integer N as an input and returns the > Nth composite number which is NOT divisible by 2. For instance, composite2(3) returns > 21 while composite2(5) returns 27.
I don't have access to your labsheet, so forgive me if I'm repeating information that you already know.
When faced with a problem I don't understand I find it's important to think about the elements of the problem and try to follow along with an example.
So let's start off first with the definition of a composite number (I've taken this one from Wikipedia):
>"A composite number is a positive integer that can be formed by multiplying two smaller >positive integers. Equivalently, it is a positive integer that has at least one divisor >other than 1 and itself."
So, a composite n is a number for which the following conditions hold:
1. n is not prime.
2. n is not 1.
So a function generating the nth composite would simply find the nth number which is not 1 or prime.
Given that you are writing a function called composite2 I can only assume you've written a function composite which generates the nth composite number.
If that is the case composite2 need only add the condition that any composite n is not divisible by 2.
To do this should be relatively trivial. I suggest you look into the modulus operator (%) in programming and how it can be used to check if a number is divisible by two.
Hope that helps!