|
You are right, this is awful. Better is:
int Primes[]={2,3,5,7,...,113};
int size=sizeof(Primes[])/sizeof(Primes[0]);
int i=0;
while (i<size)
{
if (num==Primes[i]) return true;
}
return false;
Last modified: 21hrs 3mins after originally posted --
Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]
|
|
|
|
|
I dunno, maybe a switch statement.
|
|
|
|
|
Don't you just love infinate loops?
|
|
|
|
|
(Pssst, I think he used a <, but didn't remember to use < when he posted.)
-- modified at 20:41 Thursday 14th June, 2007
int Primes[]={2,3,5,7,...,113};
int size=sizeof(Primes[])/sizeof(Primes[0]);
int i=0;
while (i<size)
{
if (num==Primes[i]) return true;
}
return false;
But you're right, it lacks i++ .
|
|
|
|
|
That is again Horror Coding
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
Your nick is a coding horror by itself! Make it small like all the other please
|
|
|
|
|
Thank you sir ..I did that...
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
MSVC++ generates an error on the above code.;)
// why not having then: int Primes[]={2,3,5,7,...};
|
|
|
|
|
Beh, any real programmer would write it like this:
int primes[] = {2,3,5,...133};
const int* end = primes + sizeof(primes)/sizeof(int);
return (std::find(primes, end, num) != end);
|
|
|
|
|
And any real engineer would build something like this:
bool IsPrime(int i)
{
if (i<2)
return false;
if (i == 2)
return true;
if (i%2 == 1)
return true;
else
return false;
}
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Ah, but in .net (C#) we can use a (generic) dictionary and an enum to further extend the required functionality...
public static partial class NumberKeeper
{
[System.FlagsAttribute]
public enum NumberProperty
{
None = 0
,
Odd = 1
,
Even = 2
,
Positive = 4
,
Negative = 8
,
Prime = 16
,
PowerOfTwo = 32
,
Square = 64
,
Cube = 128
,
Fibonacci = 256
,
Factorial = 512
} ;
public static readonly System.Collections.Generic.Dictionary<int,NumberProperty> Numbers ;
static NumberKeeper
(
)
{
Numbers = new System.Collections.Generic.Dictionary<int,NumberProperty>() ;
Numbers.Add ( 0 , NumberProperty.None ) ;
Numbers.Add ( 1 , NumberProperty.Odd | NumberProperty.Positive | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci | NumberProperty.Factorial ) ;
Numbers.Add ( 2 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Prime | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci | NumberProperty.Factorial ) ;
Numbers.Add ( 3 , NumberProperty.Odd | NumberProperty.Positive | NumberProperty.Prime | NumberProperty.Fibonacci ) ;
Numbers.Add ( 4 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Square | NumberProperty.PowerOfTwo ) ;
Numbers.Add ( 5 , NumberProperty.Odd | NumberProperty.Positive | NumberProperty.Prime | NumberProperty.Fibonacci ) ;
Numbers.Add ( 6 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Factorial ) ;
Numbers.Add ( 7 , NumberProperty.Odd | NumberProperty.Positive | NumberProperty.Prime ) ;
Numbers.Add ( 8 , NumberProperty.Even | NumberProperty.Positive | NumberProperty.Cube | NumberProperty.PowerOfTwo | NumberProperty.Fibonacci ) ;
Numbers.Add ( 9 , NumberProperty.Odd | NumberProperty.Positive | NumberProperty.Square ) ;
Numbers.Add ( 10 , NumberProperty.Even | NumberProperty.Positive ) ;
Numbers.Add ( -1 , NumberProperty.Odd | NumberProperty.Negative ) ;
Numbers.Add ( -2 , NumberProperty.Even | NumberProperty.Negative ) ;
Numbers.Add ( -3 , NumberProperty.Odd | NumberProperty.Negative ) ;
Numbers.Add ( -4 , NumberProperty.Even | NumberProperty.Negative ) ;
Numbers.Add ( -5 , NumberProperty.Odd | NumberProperty.Negative ) ;
Numbers.Add ( -6 , NumberProperty.Even | NumberProperty.Negative ) ;
Numbers.Add ( -7 , NumberProperty.Odd | NumberProperty.Negative ) ;
Numbers.Add ( -8 , NumberProperty.Even | NumberProperty.Negative | NumberProperty.Cube ) ;
Numbers.Add ( -9 , NumberProperty.Odd | NumberProperty.Negative ) ;
Numbers.Add ( -10 , NumberProperty.Even | NumberProperty.Negative ) ;
return ;
}
private static bool
HasProperty
(
int Subject
,
NumberProperty Property
)
{
return ( Numbers.ContainsKey ( Subject ) && ( ( Numbers [ Subject ] & Property ) == Property ) ) ;
}
public static bool
IsPrime
(
int Subject
)
{
return ( HasProperty ( Subject , NumberProperty.Prime ) ) ;
}
}
(I'd also add XML serialization of the list.)
Which begs the question: "Sophisticated or sophomoric?"
|
|
|
|
|
Is this real c# code or just a joke? I don't know the language but it looks pretty ugly to me...
We're in the pipe, five by five - Terran dropship.
|
|
|
|
|
Yes, just formatted "my way".
|
|
|
|
|
yowch! Glad I don't have to maintain any of your code.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
|
The guy is hidden a genious! He calculated the prime numbers up to 113 without assistance! I would have stopped at 19...
|
|
|
|
|
|
I do kinda feel sorry for him. I hope you crushed his dreams of being a programmer.
|
|
|
|
|
|
He is a genius - always staying in business with a new update of primes!!
|
|
|
|
|
ROFL ! I REALLY hope that he doesn't get hired to program an elevator controller or people will will wait a long time before getting to their level.
Je vous salue du Québec !
|
|
|
|
|
During starting my career as VC programmer and my first multithread program
I used the while loop and global variable to wait for thread to finish.
I always recall it when ever I write WaitForSingleObject after that.
<br />
DWORD dwStart = GetTickCount();<br />
<br />
while( m_sManagerNode.IsEmpty() == FALSE ){<br />
if( GetTickCount() - dwStart >= 60000 ){<br />
bReturn = FALSE; <br />
break;<br />
}<br />
}<br />
return( TRUE );<br />
Regards
Anil
|
|
|
|
|
|
Well, you made the lazy CPU working...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
|