|
Nah, since for most of their test data it returned good results.
|
|
|
|
|
Waaaay back in college, in my first Pascal class, we were assigned the task of writing a program to perform the "Sieve of Eratosthenes"... but the assignment quoted a formula to use... which was faulty (of course). I don't know what the other students did, but I headed to the library.
If I ever teach (and I'd like to) I would assign the problem with extra credit to whomever builds the largest sieve. Wouldn't this be a good Friday programming quiz?
|
|
|
|
|
PIEBALDconsult wrote: Wouldn't this be a good Friday programming quiz?
I miss those.
I leave my office a bit early on Friday evenings and don't get to see the Friday quizzes because they're posted later in the day. (I don't have a computer at home.)
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
He does not need any computer to find prime numbers
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
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 !
|
|
|
|
|