12,350,971 members (33,090 online)
Rate this:
See more:
Can you help me to get the Composite Odd Number using function..

Posted 22-Feb-13 4:14am
Edited 22-Feb-13 4:54am
RyanDev211.3K
v3
CHill60 22-Feb-13 10:27am

Which composite odd number? There are an infinite number of them
Christine Segui 22-Feb-13 11:55am

actually, this is my actual problem

I need to input an Odd composite number then the answer must be in this form
Q = p + 2 * b^2
where: Q = is the input number
P and B must be an Prime number

For example:
25 = 7 + 2 * 3 ^ 2
25 = 25

I have my code BUT I only have ONE Problem, i can only input an ODD number which is not searching if its composite number.
ryanb31 22-Feb-13 10:53am

Yes, we would love to help. Where are you stuck?
Christine Segui 22-Feb-13 11:55am

actually, this is my actual problem

I need to input an Odd composite number then the answer must be in this form
Q = p + 2 * b^2
where: Q = is the input number
P and B must be an Prime number

For example:
25 = 7 + 2 * 3 ^ 2
25 = 25

I have my code BUT I only have ONE Problem, i can only input an ODD number which is not searching if its composite number.
ryanb31 22-Feb-13 11:56am

" can only input an ODD number which is not searching if its composite number." Sorry, I don't understand what this means. Can you be more clear?
Christine Segui 22-Feb-13 12:01pm

let say, I can only input an ODD num. (1,3,5,7,9,11,13,15) all numbers have remainder 1.
In that Odd numbers, I need to have only the Odd Composite number.. (Odd numbers which is Composite number also)

Rate this:

## Solution 1

using System;

{
class Program
{
/// <summary>
/// Verify if given number is prime.
/// </summary>
/// <param name="number"></param>
/// <returns>true if number is prime, false otherwise.</returns>
{
int divisionCount = 0;
int index_number = number;

while (index_number > 0)
{
if (number % index_number == 0)
divisionCount++;

index_number--;
}

return (divisionCount == 2);
}

/// <summary>
/// Aplication entry point
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//by definition, composite odd number is a number that is not prime
for (int number = 1; number < 100; number++)
{
Console.WriteLine("prime number -> {0}", number);
else
Console.WriteLine("composite odd number -> {0}", number);
}

}
}
}
v2
Dave Kreskowiak 22-Feb-13 13:07pm

Curious. How long would it take your code to determine if 3495845893 is prime?
Though it's small and easy to understand, your method is inefficient and does a lot of math it doesn't need to do. You only need to check values up to the the Sqrt of the candidate value. You can also skip LOTS of test values. This is the function I use:

Public Function IsPrime(ByVal value As Integer) As Boolean
If value = 2 Or value = 3 Then Return True

If value < 2 Or value Mod 2 = 0 Then Return False

If value < 9 Then Return True

If value Mod 3 = 0 Then Return False

Dim i As Integer = CInt(Math.Sqrt(value))
Dim j As Integer = 5
While j <= i
If value Mod j = 0 Then Return False
If value Mod (j + 2) = 0 Then Return False
j += 6
End While

Return True
End Function

Thanks for improving the performance of the code. My purpose was to explain in a simple way how the problem could be solved.

Top Experts
Last 24hrsThis month
 OriginalGriff 345 Sergey Alexandrovich Kryukov 223 Dave Kreskowiak 175 JayantaChatterjee 85 Tabish Mateen 75
 OriginalGriff 8,845 Sergey Alexandrovich Kryukov 6,189 Dave Kreskowiak 3,009 ppolymorphe 2,256 Richard MacCutchan 2,199