Downfall - "Droppin bombs since '04"

  • home
  • forums
  • calendar
  • gallery
  • handbook
  • raid hub
  • epgp
Home › Forums › World of Warcraft General › Everything Else

for programming nerds ONLY!

Vitriola's picture

Vitriola — Thu, 07/30/2009 - 08:24

Where I work, we often bet on programming puzzles.  The latest bet was who could write a program to find all the prime numbers to a million that ran the fastest.  While I primarily program in Delphi and Java, I really like ruby.  Ruby is interpreted and I do not have the really fast version but I won by a large margin.  Mine ran in under half a second.  So, in the throes of my happy dance, I post the final code here. 

If you are not a for real programmer, look away and save yourself NOW!

 

require 'benchmark'

highestPrime = 1_000_000
highestMultiple = Math.sqrt(highestPrime).to_i
primes = Array.new(highestPrime, true)

Benchmark.bm do |x|
  x.report('primes') do
    currentNum = 3
    while currentNum < highestMultiple do
      (currentNum*currentNum).step(highestPrime, currentNum * 2) { |i| primes[i] = false }
      begin currentNum += 2 end until primes[currentNum]
    end
  end
end

# the print out
numWidth = highestPrime.to_s.length + 1
print 2.to_s.rjust(numWidth)
col = 2
3.step(highestPrime, 2) do |i|
  if primes[i]
    print i.to_s.rjust(numWidth)
    col += 1
  end
  if col == 20
    col = 1
    puts ""
  end
end
 

‹ Endgame comparison - FFXI vs. WoWdallas area folks ›
  • Everything Else
Vitriola's picture

the sincerest form of flattery

Vitriola — Thu, 07/30/2009 - 11:33

I can see that you are trying to translate my algorithm.  FYI, what won it for me was that I had optimizations that he did not consider.

First, when you are starting from a known odd prime, the first number you tick off as a multiple is its square.  Any multiples lower than that will be covered by other primes.  That means that

for (int i = currentPrime+currentPrime; i < HIGHEST_PRIME; i+= currentPrime)

should be currentPrime*currentPrime not currentPrime+currentPrime.  Also, as every other multiple is a multiple of 2,

i+= currentPrime

can be optimized to

i+= currentPrime * 2

thus halving the number of steps.  He also did not consider that he only needed to go as far as the square root of the max value.  I left it as a mathematical function rather than hard coding because I tried it out with various max numbers.

He was also doing a lot of floating point math, where I do none.  I know that the same algorithm will run faster if compiled, but we have few speed contests here.  More often it is a matter of code golfing.  (smallest code to git R done)

C# Primes

Knoppix (not verified) — Thu, 07/30/2009 - 09:15

using System;

namespace Primes
{
class Program
{
static void Main(string[] args)
{
for (long i = 2; i <= 1000000; i++)
{
if ((i % 2 != 0) || (i == 2))
{
Console.WriteLine("{0} ", i);
}
}

Console.WriteLine("Complete!");
}
}
}

Take our the Console.WriteLine in the "if" statement and the code executes instantly, unless you want to print every prime number from 0 to 1,000,000 to the screen. Writting to the GUI this many times no matter what language is slow.

saedo's picture

Isn't yours just listing the

saedo — Thu, 07/30/2009 - 09:28

Isn't yours just listing the set of odd numbers and 2?

You are right, trying to

Knoppix (not verified) — Thu, 07/30/2009 - 09:51

You are right, trying to indent this chopped off the upper part of the code.  I will post this later, time to do my real work.

  • « first
  • ‹ previous
  • 1
  • 2

Random image

Knoppix in Bora Bora

Navigation

  • Content
  • Raid Parses
  • The Downfall Handbook
  • User list
  • Polls
  • Groups
  • Popular content

Who's new

  • Babyconan
  • Grantlius
  • Ecology
  • fumabomb
  • firstslacker

Latest image

PMS Logo for Legion

Archives

  • September 2016 (1)
  • August 2016 (5)
  • July 2016 (8)
  • June 2016 (2)
  • May 2016 (1)
  • March 2016 (3)
  • February 2016 (2)
  • January 2016 (5)
  • August 2015 (1)
  • May 2015 (3)

Recent comments

  • App closed with extreme
    7 hours 17 min ago
  • Guild App closed at his
    4 weeks 1 day ago
  • Once we get the new site,
    7 weeks 1 day ago
  • Is the time frame posted in server time?
    7 weeks 2 days ago
  • Awesome. Thanks. Once I
    9 weeks 1 day ago
  • I'd be happy to stick around
    9 weeks 1 day ago
  • Possibly
    9 weeks 1 day ago
  • More necro
    10 weeks 2 days ago
  • Story time!
    11 weeks 3 days ago
  • I;ve downladed drop box, if
    13 weeks 17 hours ago

Active forum topics

  • April 1st 2006
  • Nostalarious: "Blizzard wants Vanilla servers"
  • Galaga 13/13 Heroic by Deposing A Tyrant
  • Poker Face Achieves in one night! Glory of Hellfire Raider
  • Glory of The Hellfire Raider
  • Galaga Destroys Heroic Archimonde AOTC 12/13H
  • Galaga takes out Thomas the Train (7/10 H BRF)
more

Recent blog posts

  • So I guess this is the end...
  • Shaedo's Soloing Extravaganza
  • Shaedo UI Extravaganza
  • Big site updates this weekend!
  • This is for all the Hearthstone players out there
  • My First Full Day on Beta
  • State Of <Downfall> 2014
  • Scaron taking a break
  • How Gorgonnash's Connections went
  • Let's Talk about Mythic Raids
more

Poll

What do you think about no flying for an entire expansion?
YES) It makes me feel more immersed in the game lore.
15%
YES) It encourages more PvP! (Please explain how in comments)
5%
YES) I miss riding my ground mounts.
9%
MAYBE) If you make all of my flying mounts able to be ground too.
8%
MAYBE) if ground mounts are sped up to 200% speed (or higher)
26%
NO) I don't have enough time when I'm on to be riding everywhere
7%
NO) I have spent too much time getting various levels of flying to be wasting it.
11%
NO) It encourages more PVP! (please explain in the comments)
0%
NO) I love flying.
19%
Total votes: 74
  • 38comments
  • Older polls

Popular content

All time:

  • Professor Putricide Strategy Page
  • Rotface Strategy Guide
  • Arthas, the Lich King Strategy Page
  • Sindragosa Strategy Page
  • Toravon the Ice Watcher Strategy Guide
  • home
  • forums
  • calendar
  • gallery
  • handbook
  • raid hub
  • epgp

I notice your oeuvre is monochromatic.
This site is proudly powered by Drupal.