at least he tried
-
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. -
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[^]
Don't you just love infinate loops?:)
-
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: :D
bool IsPrime(int i)
{
if (i<2)
return false;
if (i == 2)
return true;
if (i%2 == 1)
// All odd numbers are prime:
// 3 ... prime
// 5 ... prime
// 7 ... prime
// 9 ... measuring fault
// 11... prime
// 13... prime
// and so on...
return true;
else
return false;
}Regards, mav -- Black holes are the places where God divided by 0...
-
as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :
if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
{
return true;
}
else
{
return false;
}when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?
me, myself and my blog - loadx.org ericos g.
The guy is hidden a genious! He calculated the prime numbers up to 113 without assistance! I would have stopped at 19... :-)
-
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++
. -
(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 :laugh:
Regards, Sylvester G sylvester_g_m@yahoo.com
-
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);
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 /\* et cetera \*/ } ; 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.
-
The guy is hidden a genious! He calculated the prime numbers up to 113 without assistance! I would have stopped at 19... :-)
andre-ladeira wrote:
He calculated the prime numbers up to 113 without assistance!
:cough: Unlikely ->- Pascal - Response[^] http://en.wikipedia.org/wiki/Prime_number[^]
-
as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :
if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
{
return true;
}
else
{
return false;
}when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?
me, myself and my blog - loadx.org ericos g.
I do kinda feel sorry for him. I hope you crushed his dreams of being a programmer.
-
as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :
if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
{
return true;
}
else
{
return false;
}when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?
me, myself and my blog - loadx.org ericos g.
Wow
-
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 /\* et cetera \*/ } ; 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.
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.
-
wikipedia stops at 113 too :) http://en.wikipedia.org/wiki/Prime_number[^]
- Pascal - wrote:
wikipedia stops at 113 too
13 is considered bad unlucky number. Is 113 fall under this category also? :-D
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
And, in addition to being very limited in the range of input values, the result isn't correct, either. <SmartassMode> 1 is not a prime number, by definition. </SmartassMode> ;P
Regards, mav -- Black holes are the places where God divided by 0...
mav.northwind wrote:
1 is not a prime number, by definition.
It is qualified as Unique Number right?
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
Should have told him he has to use this list: The first 10000 prime numbers[^]
Brent
dbrenth wrote:
The first 10000 prime numbers[^]
And any of the following things would have happened for sure: 1) Typing the if-else clause for 10000 numbers, he would broken the keyboard. 2) Typing the if-else clause for 10000 numbers, he would have broken down and ambulance should have been requested. It is an unnecessary headache, at least in this case right?
Vasudevan Deepak Kumar Personal Homepage Tech Gossips
-
Ask him to write a function that returns weather or not a given number is even :D
Giorgi Dalakishvili wrote:
Ask him to write a function that returns weather or not a given number is even
And for sure, the developer would written the code which would have thrown
System.StackOverFlowException
:-DVasudevan Deepak Kumar Personal Homepage Tech Gossips
-
as an assignment, i asked for a program that we've all been through, a function that returns weather or not a given number is prime. in one case, i was given the following :
if(num== 1 || num== 2 || num== 3 || num== 5 || num== 7 || num== 11 || num== 13 || num== 17 || num== 19 || num== 23 || num== 29 || num== 31 || num== 37 || num== 41 || num== 43 || num== 47 || num== 53 || num== 59 || num== 61 || num== 67 || num== 71 || num== 73 || num== 79 || num== 83 || num== 89 || num== 97 || num== 101 || num== 103 || num== 107 || num== 109 || num== 113)
{
return true;
}
else
{
return false;
}when i asked him why he stopped at 113 (since it was the only thing i thought of asking) he said, in a tired voice, "I couldn't think of any more numbers" do you, or do you not feel sorry for him?
me, myself and my blog - loadx.org ericos g.
He is a genius - always staying in business with a new update of primes!!
-
wikipedia stops at 113 too :) http://en.wikipedia.org/wiki/Prime_number[^]
Interesting how many unproved conjectures there are.
Kevin
-
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".
-
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
-
That is again Horror Coding :laugh:
Regards, Sylvester G sylvester_g_m@yahoo.com
Your nick is a coding horror by itself! Make it small like all the other please :)
**
xacc.ide-0.2.0.75 - now with C# 3.5 support and Navigation Bar!
**