Click here to Skip to main content
11,706,455 members (45,474 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Can you help me to get the Composite Odd Number using function..


Thank you in advance
Posted 22-Feb-13 4:14am
Edited 22-Feb-13 4:54am
RyanDev168K
v3
Comments
CHill60 at 22-Feb-13 10:27am
   
Which composite odd number? There are an infinite number of them
Christine Segui at 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 at 22-Feb-13 10:53am
   
Yes, we would love to help. Where are you stuck?
Christine Segui at 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 at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
        }
    }
}
  Permalink  
v2
Comments
Dave Kreskowiak at 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.

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

  Print Answers RSS
0 OriginalGriff 197
1 Sergey Alexandrovich Kryukov 141
2 chainerlt 110
3 ppolymorphe 71
4 Mika Wendelius 70
0 OriginalGriff 9,070
1 Sergey Alexandrovich Kryukov 8,413
2 CPallini 5,189
3 Maciej Los 4,746
4 Mika Wendelius 3,676


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100