| Author |
Message |
Algorithms Dragon
Joined: 21 Oct 2004 Posts: 343 Location: Florida
|
Posted: Wed Jul 20, 2005 2:45 pm Post subject: Classic - Prime Numbers |
|
|
Here is a classic, print off prime numbers from 0-100 or 0-n how ever high you feel like going.
| Code: |
# Python
n = 100
for i in range(n):
prime = 1
if i == 1 or i == 0:
continue
for j in range(n):
if j == 0 or j == 1 or j == i:
continue
if i % j == 0:
prime = 0
if prime:
print i
|
| Code: |
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
|
|
|
| Back to top |
|
 |
Cerulean Gokenin
Joined: 22 Oct 2004 Posts: 742 Location: London, England
|
Posted: Wed Jul 20, 2005 4:04 pm Post subject: |
|
|
| *had forgotten this forum exists* |
|
| Back to top |
|
 |
WannaBe Wiggles
Joined: 22 Oct 2004 Posts: 714 Location: CA
|
Posted: Wed Jul 20, 2005 4:17 pm Post subject: |
|
|
| Code: | 'Visual Basic 6
Dim i As Long, c As Long
Dim max_range As Long
Dim is_prime As Byte
max_range = 500
For i = 1 To max_range
is_prime = 1
For c = 2 To i - 1
If (i Mod c) = 0 Then
is_prime = 0
Exit For
End If
Next c
If is_prime = 1 Then Debug.Print i
Next i |
| Code: |
<?php
//PHP
$max_range = 500;
for( $i=1;$i<=$max_range;$i++ )
{
$is_prime = true;
for( $c=2; $c<$i; $c++ )
{
if( $i % $c == 0 )
{
$is_prime = false;
break;
}
}
if( $is_prime ){ echo( $i . '<br />' ); }
}
?> |
| Code: | <html><!--java script-->
<head><title>Primes</title>
<script>
function echo_prime()
{
var max_range=500;
var i=0;
var c=0;
var is_prime=1;
for( i=1;i<=max_range;i++ )
{
is_prime = 1;
for( c=2; c<i; c++ )
{
if( i % c == 0 )
{
is_prime = 0;
break;
}
}
if( is_prime == 1 ){ window.document.getElementById("div_echo").innerHTML += i + "<br />"; }
}
}
</script>
</head>
<body onload="echo_prime();">
<div id="div_echo"></div>
</body>
</html> |
edit:added java script |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Wed Jul 20, 2005 4:46 pm Post subject: |
|
|
C++
| Code: |
#include <iostream>
struct sNumber
{
int iPrimeNumber;
sNumber *pNext;
};
int _tmain(int argc, _TCHAR* argv[])
{
sNumber *pStart = NULL;
sNumber *pEnd = NULL;
std::cout << "Please enter the number to stop at: ";
int iMaxNumber = 0;
std::cin >> iMaxNumber;
pStart = new sNumber();
pStart->iPrimeNumber = 2;
pStart->pNext = NULL;
pEnd = pStart;
for (int i = 2; i < iMaxNumber; i++)
{
sNumber *pCur = pStart;
bool bPrime = true;
while (pCur)
{
if (i % pCur->iPrimeNumber == 0)
{
bPrime = false;
break;
}
pCur = pCur->pNext;
}
if (bPrime)
{
sNumber *pNewItem = new sNumber();
pNewItem->iPrimeNumber = i;
pNewItem->pNext = NULL;
pEnd->pNext = pNewItem;
pEnd = pNewItem;
std::cout << i << std::endl;
}
}
// cleanup
sNumber *pCur = pStart;
while (pCur)
{
sNumber *pNext = pCur->pNext;
delete pCur;
pCur = pNext;
}
return 0;
} |
|
|
| Back to top |
|
 |
intrest86 Samurai++
Joined: 31 Dec 1969 Posts: 64 Location: Johns Hopkins
|
Posted: Wed Jul 20, 2005 5:45 pm Post subject: |
|
|
| Code: | static void Main(string[] args)
{
bool[] num = new bool[101];
num[0] = num[1] = true;
for (int i = 2; i<101; i++)
{
if (!num[i])
{
System.Console.WriteLine(i);
for (int k = i+i; k<101; k+=i)
num[k] = true;
}
}
} |
I hope my incredibly short and efficient answer makes you cry  |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Wed Jul 20, 2005 7:09 pm Post subject: |
|
|
| Quote: | | I hope my incredibly short and efficient answer makes you cry |
I don't get paid for short and efficient and besides yours is .net so efficency gets thrown out the window  |
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
Posted: Wed Jul 20, 2005 7:16 pm Post subject: |
|
|
| intrest86 wrote: | | Code: | static void Main(string[] args)
{
bool[] num = new bool[101];
num[0] = num[1] = true;
for (int i = 2; i<101; i++)
{
if (!num[i])
{
System.Console.WriteLine(i);
for (int k = i+i; k<101; k+=i)
num[k] = true;
}
}
} |
I hope my incredibly short and efficient answer makes you cry  |
Gives me syntax errors in my VBScript interpreter.  |
|
| Back to top |
|
 |
intrest86 Samurai++
Joined: 31 Dec 1969 Posts: 64 Location: Johns Hopkins
|
Posted: Wed Jul 20, 2005 8:04 pm Post subject: |
|
|
| bdi wrote: |
Gives me syntax errors in my VBScript interpreter.  |
Har har
It's C#, and since it only has one memory allocation it is far from inefficient . And not a single divide, so eat that  |
|
| Back to top |
|
 |
WbHack Bushi
Joined: 27 Jan 2005 Posts: 335 Location: Seattle
|
Posted: Wed Jul 20, 2005 10:42 pm Post subject: |
|
|
Basic:
| Code: | 10 ? "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 51 67 71 73 79 83 89 97"
20 End |
(seriously though... this is a math thing... if someone doesn't do COBOL and/or FORTRAN, there's something wrong) |
|
| Back to top |
|
 |
WbHack Bushi
Joined: 27 Jan 2005 Posts: 335 Location: Seattle
|
Posted: Wed Jul 20, 2005 10:45 pm Post subject: |
|
|
[quote="WannaBe"]
c'mon, you can't do three in one post. that's not fair.  |
|
| Back to top |
|
 |
Cerulean Gokenin
Joined: 22 Oct 2004 Posts: 742 Location: London, England
|
Posted: Thu Jul 21, 2005 5:17 am Post subject: |
|
|
[quote="WbHack"] | WannaBe wrote: |
c'mon, you can't do three in one post. that's not fair.  |
Hear hear.. C++, Python, and java script were gone by the time I came to submit my entry  |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Thu Jul 21, 2005 8:13 am Post subject: |
|
|
| Quote: | | Hear hear.. C++, Python, and java script were gone by the time I came to submit my entry |
well apparently the rest of our submissions are long and inneficient so you're welcome to post something better... |
|
| Back to top |
|
 |
WbHack Bushi
Joined: 27 Jan 2005 Posts: 335 Location: Seattle
|
Posted: Thu Jul 21, 2005 9:08 am Post subject: |
|
|
| *narf* wrote: | | well apparently the rest of our submissions are long and inneficient so you're welcome to post something better... |
hm. the way i read it, just yours.  |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Thu Jul 21, 2005 11:38 am Post subject: |
|
|
| *feels his banning finger start twitching* |
|
| Back to top |
|
 |
WannaBe Wiggles
Joined: 22 Oct 2004 Posts: 714 Location: CA
|
Posted: Thu Jul 21, 2005 11:44 am Post subject: |
|
|
man, sorry for posting three different langs, but I was bored. besides they are really all the same code.
and btw *narf*'s code isn't inneficient, it is the only one that asks the user for the range. (thats not a suck up ) |
|
| Back to top |
|
 |
Ankou Spam Mod
Joined: 22 Oct 2004 Posts: 1201 Location: Wisconsin
|
Posted: Thu Jul 21, 2005 3:09 pm Post subject: |
|
|
Okay just thought I'd toss in the Sieve of Eratosthenes method. I didn't feel like trying to do the Sieve of Atkin method right now, maybe a bit later.
With Comments:
| Code: | <?php
// Highest value to check
$MAX = 100;
$HIGH_CHECK = floor(sqrt($MAX));
$REMAIN = array();
$PRIMES = array();
echo "Checking for primes b/n 1 and $MAX<br />Will only need to check to $HIGH_CHECK<br /><br />";
// Load the $REMAIN array
for($i = 2; $i <= $MAX; $i++){ array_push($REMAIN, $i); }
// Reverse array $REMAIN to be able to POP from array.
$REMAIN = array_reverse($REMAIN);
while(count($REMAIN) > 0){
// Pop the value from array $REMAIN - this value is the
// lowest remaining value in the list we need to check.
$n = array_pop($REMAIN);
// Since $n is the lowest value, it can be pushed onto
// our list of prime numbers (array $PRIMES).
array_push($PRIMES, $n);
// If the lowest value of the $REMAIN array is greater than
// the floor of the of the squareroot of our highest value,
// then we can exit out of this process.
if($n > $HIGH_CHECK) break;
// If the values remaining ($REMAIN array) are divisible by
// the lowest value of the $REMAIN array, which we popped
// off before, they can be removed from the $REMAIN array.
foreach($REMAIN as $key => $value){
if($value % $n == 0){ unset($REMAIN[$key]); }
}
}
// Merge and display array values
// Array $REMAIN is reversed so the numbers are in order.
foreach(array_merge($PRIMES, array_reverse($REMAIN)) as $show_val){
echo $show_val . "<br />";
}
?> |
Without comments:
| Code: | <?php
$MAX = 100;
$HIGH_CHECK = floor(sqrt($MAX));
$REMAIN = array();
$PRIMES = array();
echo "Checking for primes b/n 1 and $MAX<br />Will only need to check to $HIGH_CHECK<br /><br />";
for($i = 2; $i <= $MAX; $i++){ array_push($REMAIN, $i); }
$REMAIN = array_reverse($REMAIN);
while(count($REMAIN) > 0){
$n = array_pop($REMAIN);
array_push($PRIMES, $n);
if($n > $HIGH_CHECK) break;
foreach($REMAIN as $key => $value){
if($value % $n == 0){ unset($REMAIN[$key]); }
}
}
foreach(array_merge($PRIMES, array_reverse($REMAIN)) as $show_val){
echo $show_val . "<br />";
}
?> |
|
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
Posted: Thu Jul 21, 2005 3:51 pm Post subject: |
|
|
| WannaBe wrote: |
man, sorry for posting three different langs, but I was bored. besides they are really all the same code.
and btw *narf*'s code isn't inneficient, it is the only one that asks the user for the range. (thats not a suck up ) |
I thought unnecessary complexity was admired in these circles? Then again, so is efficiency. Double edged sword.
Actually, it would make a good source code sample for a linked list. |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Thu Jul 21, 2005 4:17 pm Post subject: |
|
|
| Quote: | | Actually, it would make a good source code sample for a linked list. |
I don't get what you're trying to say  |
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
Posted: Thu Jul 21, 2005 4:22 pm Post subject: |
|
|
| *narf* wrote: | | Quote: | | Actually, it would make a good source code sample for a linked list. |
I don't get what you're trying to say  |
That you're a di..*narf*
Can't say that here.  |
|
| Back to top |
|
 |
WbHack Bushi
Joined: 27 Jan 2005 Posts: 335 Location: Seattle
|
Posted: Thu Jul 21, 2005 9:50 pm Post subject: |
|
|
| ok... Now that all the normal languages are used up *ahem wannabe* .... Rube Goldberg it....... how cumbersome can you make it (that's at least semi-relevant) so that it still does the same thing? |
|
| Back to top |
|
 |
|