Click here to Skip to main content
15,042,088 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
Can you help me to get the Composite Odd Number using function..


Thank you in advance
Posted
Updated 22-Feb-13 4:54am
v3
Comments
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.
ZurdoDev 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.
ZurdoDev 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)

1 solution

C#
using System;

namespace PrimeNumber
{
    class Program
    {
        /// <summary>
        /// Verify if given number is prime.
        /// </summary>
        /// <param name="number"></param>
        /// <returns>true if number is prime, false otherwise.</returns>
        static bool IsPrimeNumber(int number)
        {
            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++)
            {
                if (IsPrimeNumber(number))
                    Console.WriteLine("prime number -> {0}", number);
                else
                    Console.WriteLine("composite odd number -> {0}", number);
            }

            Console.ReadKey();
        }
    }
}
   
v2
Comments
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:

<Extension()>
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
José Amílcar Casimiro 23-Feb-13 5:42am
   
Thanks for improving the performance of the code. My purpose was to explain in a simple way how the problem could be solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900