• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 08:11
CET 13:11
KST 21:11
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Team Liquid Map Contest #22 - Presented by Monster Energy4ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13
Community News
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool22Weekly Cups (March 9-15): herO, Clem, ByuN win32026 KungFu Cup Announcement6BGE Stara Zagora 2026 cancelled12Blizzard Classic Cup - Tastosis announced as captains18
StarCraft 2
General
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Serral: 24’ EWC form was hurt by military service Weekly Cups (March 9-15): herO, Clem, ByuN win Team Liquid Map Contest #22 - Presented by Monster Energy Weekly Cups (August 25-31): Clem's Last Straw?
Tourneys
KSL Week 87 [GSL CK] #2: Team Classic vs. Team Solar 2026 KungFu Cup Announcement [GSL CK] #1: Team Maru vs. Team herO RSL Season 4 announced for March-April
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026] Map Editor closed ?
External Content
The PondCast: SC2 News & Results Mutation # 517 Distant Threat Mutation # 516 Specter of Death Mutation # 515 Together Forever
Brood War
General
ASL21 General Discussion BGH Auto Balance -> http://bghmmr.eu/ JaeDong's form before ASL Gypsy to Korea BSL Season 22
Tourneys
Small VOD Thread 2.0 [Megathread] Daily Proleagues [BSL22] Open Qualifiers & Ladder Tours IPSL Spring 2026 is here!
Strategy
Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2 Fighting Spirit mining rates Zealot bombing is no longer popular?
Other Games
General Games
Nintendo Switch Thread Path of Exile General RTS Discussion Thread Stormgate/Frost Giant Megathread Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion The Story of Wings Gaming
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Mexico's Drug War Canadian Politics Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion Tokyo Olympics 2021 Thread General nutrition recommendations Cricket [SPORT]
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 7663 users

The Big Programming Thread - Page 13

Forum Index > General Forum
Post a Reply
Prev 1 11 12 13 14 15 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Cloud
Profile Blog Joined November 2004
Sexico5880 Posts
Last Edited: 2010-07-15 12:39:24
July 15 2010 12:35 GMT
#241
Hey, could someone help me with problem 10? (find the sum of all primes below two million).

My code works just fine for small numbers but when it goes above a certain number it just displays a bunch of crap. I wanna know if I'm declaring my types wrong or if there's something else wrong.

+ Show Spoiler +


The function primo checks if it's a prime. If it's a prime it returns a 1 and the main function sums it to 17 (I skip 2, 3, 5 and 7). All prime numbers apart from 2 and 3 are of the form 6*i +/- 1, and to check if it's a prime you don't need to go above the sqrt of the number.

#include <stdio.h>
#include <math.h>

#define MAX 2000000

int primo(int);

int main(void)
{
int i;
long int suma = 17;

for (i= 2; i * 6 + 1 < MAX; i++) {
if (primo((i * 6) + 1) == 1)
suma += ((i * 6) + 1);
}

for (i = 2; i * 6 - 1 < MAX; i++) {
if (primo((i * 6) - 1) == 1)
suma += ((i * 6) - 1);
}

printf("%d", suma);

return 0;
}

int primo(int num)
{
int i;
float j;

if (num % 2 == 0)
return 0;

if (num % 3 == 0)
return 0;

j = sqrt(num);

for (i = 1; i * 6 + 1 <= j; i++) {
if (num % (i * 6 + 1) == 0)
return 0;
}

for (i = 1; i * 6 - 1 <= j; i++) {
if (num % (i * 6 - 1) == 0)
return 0;
}

return 1;
}

BlueLaguna on West, msg for game.
Adeny
Profile Blog Joined January 2009
Norway1233 Posts
Last Edited: 2010-07-15 15:54:14
July 15 2010 13:41 GMT
#242
I can't remember the exact value, but an int can only store so much, and it's around 2 billion somewhere. Try changing your ints to unsigned int. What this does, is it changes your number range from -2 billion to +2 billion, to 0 to 4 billion instead.

Normally your int has 1 bit that's designated to tell if it's a positive or negative number, so you can only store up to 2^31 (which makes 32 bit total). With unsigned, you get the range of 2^32. I don't know if that's clear or not. There are also some other datatypes that can store larger numbers, but if you want REALLY large numbers in C/++, you're going to need a custom class/library, like GMP Big Num.

Oh, and for optimizing primes, you should store the primes in an array, and loop through it. You also only need to check upwards to the square root of n. Oh in your for loops, you can use whatever you like, so starting at 3 and doing i += 2 is twice as (approx) as i++, because you know all even numbers are divisible by 2.

Edit: Billion, not Million.
catamorphist
Profile Joined May 2010
United States297 Posts
July 15 2010 14:22 GMT
#243
The above poster is right on except that 32 bits is good up to 2 billion, not 2 million.
http://us.battle.net/sc2/en/profile/281144/1/catamorphist/
Adeny
Profile Blog Joined January 2009
Norway1233 Posts
July 15 2010 15:53 GMT
#244
Oops, my bad. Thanks for the correction.
EpiK
Profile Blog Joined January 2007
Korea (South)5757 Posts
Last Edited: 2010-07-15 23:07:18
July 15 2010 22:38 GMT
#245
Poll: Which language do you use the most often?

C++ (10)
 
23%

C# (9)
 
21%

Java (8)
 
19%

C (7)
 
16%

Python (5)
 
12%

Perl (1)
 
2%

Ruby (1)
 
2%

Delphi (1)
 
2%

Other (please specify) (1)
 
2%

PHP (0)
 
0%

Visual Basic (0)
 
0%

JavaScript (0)
 
0%

43 total votes

Your vote: Which language do you use the most often?

(Vote): C
(Vote): Java
(Vote): C#
(Vote): C++
(Vote): PHP
(Vote): Visual Basic
(Vote): Python
(Vote): Perl
(Vote): JavaScript
(Vote): Ruby
(Vote): Delphi
(Vote): Other (please specify)



C# mostly for me. I've been using a lot of python too lately for a school project I'm doing with the panda3d engine.
UdderChaos
Profile Blog Joined February 2010
United Kingdom707 Posts
July 15 2010 22:49 GMT
#246
lol VB isn't a real language, it's like the mac of the programming languages.
Nunquam iens addo vos sursum
dimfish
Profile Blog Joined February 2010
United States663 Posts
July 15 2010 22:56 GMT
#247
I voted for C++, but really I mostly use C and only C++ when C is decidedly inconvenient (like not letting me declare local variables right before I want to use them). I'm really curious to see how this poll turns out. Maybe the poll should be what you use the most for work projects and one for what you use on your own time.
Count9
Profile Blog Joined May 2009
China10928 Posts
July 15 2010 23:05 GMT
#248
Perl all the way, mostly because I don't want to be able to read my programs when I'm done so I don't have to debug. I use tcl a lot too.
mmp
Profile Blog Joined April 2009
United States2130 Posts
Last Edited: 2010-07-15 23:16:55
July 15 2010 23:10 GMT
#249
On July 15 2010 21:35 Cloud wrote:
Hey, could someone help me with problem 10? (find the sum of all primes below two million).

My code works just fine for small numbers but when it goes above a certain number it just displays a bunch of crap. I wanna know if I'm declaring my types wrong or if there's something else wrong.

+ Show Spoiler +


The function primo checks if it's a prime. If it's a prime it returns a 1 and the main function sums it to 17 (I skip 2, 3, 5 and 7). All prime numbers apart from 2 and 3 are of the form 6*i +/- 1, and to check if it's a prime you don't need to go above the sqrt of the number.

#include <stdio.h>
#include <math.h>

#define MAX 2000000

int primo(int);

int main(void)
{
int i;
long int suma = 17;

for (i= 2; i * 6 + 1 < MAX; i++) {
if (primo((i * 6) + 1) == 1)
suma += ((i * 6) + 1);
}

for (i = 2; i * 6 - 1 < MAX; i++) {
if (primo((i * 6) - 1) == 1)
suma += ((i * 6) - 1);
}

printf("%d", suma);

return 0;
}

int primo(int num)
{
int i;
float j;

if (num % 2 == 0)
return 0;

if (num % 3 == 0)
return 0;

j = sqrt(num);

for (i = 1; i * 6 + 1 <= j; i++) {
if (num % (i * 6 + 1) == 0)
return 0;
}

for (i = 1; i * 6 - 1 <= j; i++) {
if (num % (i * 6 - 1) == 0)
return 0;
}

return 1;
}



When doing primality calculations you want to use bigint libraries to avoid overflowing (an array of ints or a string/char* representation - makes things a little clumsy but saves a real headache) - you can also use Fermat's Little Theorem to test primality in constant time - and maintain a database of Carmichael numbers for small n. Most bigint libraries should already have probable primality implemented for you, although it's very easy to write yourself.
Source: http://en.wikipedia.org/wiki/Probable_prime
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
Gogleion
Profile Blog Joined June 2009
United States534 Posts
Last Edited: 2010-07-15 23:16:12
July 15 2010 23:13 GMT
#250
Java fighting! Its gotten seriously good over the past 5 years and is now extremely powerful, and not a 'toy' language that I hear a lot of people call it. I'm saddened when I see C# is in first... D:

I use PHP a lot too, but that's just because of how I do web development. I don't necessarily like PHP as a language though.

COBOL isn't on the list, it is mostly outdated, but professional COBOL programmers make serious money since its really good for databases and not a lot of people are good at it.

Also Pascal isn't on there. I'm pretty sure either bwchart or chaoslauncher was made in pascal. Also missing is J, a relatively obscure but powerful language in the hands of people that know it. Also QBasic, ASP.net, MySQL, HTML, and XML/AJAX (I know JavaScript is on there already) are missing to name a few more.

EDIT: I'm really happy to see eulers on here. If anyone else is interested I could probably make a thread discussing them.
EffOrt. That is all.
TanGeng
Profile Blog Joined January 2009
Sanya12364 Posts
July 15 2010 23:17 GMT
#251
C because of Linux driver development.
Moderator我们是个踏实的赞助商模式俱乐部
Craton
Profile Blog Joined December 2009
United States17281 Posts
Last Edited: 2010-07-15 23:52:18
July 15 2010 23:52 GMT
#252
C# with ASP.NET for all my work right now and a lot of VB.NET for school, but I think I'm done with VB.NET here on out.
twitch.tv/cratonz
catamorphist
Profile Joined May 2010
United States297 Posts
Last Edited: 2010-07-16 00:00:00
July 15 2010 23:58 GMT
#253
C#, F#, T-SQL at work, and mostly Clojure at home. (For a long time before six months ago I would have said mostly PLT Scheme at home, but Clojure really 0wns.)
http://us.battle.net/sc2/en/profile/281144/1/catamorphist/
RoTaNiMoD
Profile Blog Joined January 2004
United States558 Posts
July 16 2010 00:09 GMT
#254
C++ all the way. Complete memory control laughs in the face of your puny garbage collection. I am interested to see how this poll will turn out and am happy with the setup. The main languages are all there with enough room for a decent amount of less prominent languages too. MySQL/HTML/XML, though certainly intrinsic parts of development processes, aren't actually programming languages and thus are good to be left off.
Craton
Profile Blog Joined December 2009
United States17281 Posts
Last Edited: 2010-07-16 00:57:21
July 16 2010 00:22 GMT
#255
We could make a Euler's blog rather than it's own thread, but I'm down for either.

Got the prime one right on first try, but hella slow. (Visual C#)
Time: 00:04:35.2571679
twitch.tv/cratonz
SoLaR[i.C]
Profile Blog Joined August 2003
United States2969 Posts
July 16 2010 00:27 GMT
#256
Anybody have a suggestion for a FORTRAN compiler?
TanGeng
Profile Blog Joined January 2009
Sanya12364 Posts
Last Edited: 2010-07-16 00:33:12
July 16 2010 00:29 GMT
#257
On July 16 2010 09:09 RoTaNiMoD wrote:
C++ all the way. Complete memory control laughs in the face of your puny garbage collection. I am interested to see how this poll will turn out and am happy with the setup. The main languages are all there with enough room for a decent amount of less prominent languages too. MySQL/HTML/XML, though certainly intrinsic parts of development processes, aren't actually programming languages and thus are good to be left off.


this

There is more code to write though. I know a lot of languages and use them all depending on existing code base, performance requirements, and time constraints.

The argument that you can bang something out really really fast, is ultra portable, or has the best library support is a strong selling point for the languages with automatic cleanup.
Moderator我们是个踏实的赞助商模式俱乐部
RoTaNiMoD
Profile Blog Joined January 2004
United States558 Posts
July 16 2010 00:34 GMT
#258
On July 15 2010 21:35 Cloud wrote:
Hey, could someone help me with problem 10? (find the sum of all primes below two million).

My code works just fine for small numbers but when it goes above a certain number it just displays a bunch of crap. I wanna know if I'm declaring my types wrong or if there's something else wrong.

+ Show Spoiler +


The function primo checks if it's a prime. If it's a prime it returns a 1 and the main function sums it to 17 (I skip 2, 3, 5 and 7). All prime numbers apart from 2 and 3 are of the form 6*i +/- 1, and to check if it's a prime you don't need to go above the sqrt of the number.

#include <stdio.h>
#include <math.h>

#define MAX 2000000

int primo(int);

int main(void)
{
int i;
long int suma = 17;

for (i= 2; i * 6 + 1 < MAX; i++) {
if (primo((i * 6) + 1) == 1)
suma += ((i * 6) + 1);
}

for (i = 2; i * 6 - 1 < MAX; i++) {
if (primo((i * 6) - 1) == 1)
suma += ((i * 6) - 1);
}

printf("%d", suma);

return 0;
}

int primo(int num)
{
int i;
float j;

if (num % 2 == 0)
return 0;

if (num % 3 == 0)
return 0;

j = sqrt(num);

for (i = 1; i * 6 + 1 <= j; i++) {
if (num % (i * 6 + 1) == 0)
return 0;
}

for (i = 1; i * 6 - 1 <= j; i++) {
if (num % (i * 6 - 1) == 0)
return 0;
}

return 1;
}



Cloud: good start, but to achieve speed-efficiency, you are going to need to make some changes. First, your primeness test. The i*6+/-1 idea is near-optimal for small n, but as n grows there becomes more and more overlap between tests (for example testing 4*6+1 -- 25 -- is redundant when you also test for 1*6-1 -- 5). Instead, store all your primes in an array of some sort and only test n for divisibility by prime numbers up to sqrt(n).

Second, your for loops could use some optimization. Generally this is no big deal, but in programs like this, where you spend an extreme amount of time looping, optimizing your statements can provide huge speed boosts. Every time you compute ( i * 6 +/- 1 ), two unnecessary evaluations are made (multiplication by 6 and addition/subtraction of 1). Simply having a variable for that value would save you several evaluations per loop-cycle. When each cycle is only 15 or so evalutions to begin with, that's huge.
lastkarrde
Profile Joined December 2009
New Zealand66 Posts
July 16 2010 00:42 GMT
#259
Python Hwaiting!

Pypy is getting faster and more featureful by the day, damn awesome.
TTD
Craton
Profile Blog Joined December 2009
United States17281 Posts
Last Edited: 2010-07-16 01:03:36
July 16 2010 00:48 GMT
#260
aha wow what a difference that makes rotan
+ Show Spoiler +

The sqrt part.

private void btnGo_Click(object sender, EventArgs e)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Int64 Total = 0;
List<int> list = new List<int>();
Boolean isPrime = true;
for (int i = 2; i <= 2000000; i++)
{
isPrime = true;
foreach (int j in list)
{
if (i % j == 0)
{
//Not a prime
isPrime = false;
break;
}
//****** Change Start ******
if (j > Math.Sqrt(i))
break;
//****** Change End ******
} //j
if (isPrime)
{
Total += i;
list.Add(i);
}
} //i

stopWatch.Stop();
txtEuler.Text = Total.ToString() + " -- Time: " + stopWatch.Elapsed;
}


Got mine down to:
Time: 00:00:01.1228236

Bolded my change. Or, I would have but you can't have bold tags in code. Added comments.


Time: 00:00:00.3553445 for #7, 10001st prime. Kinda pointless once you've got the other one done, though =/.

+ Show Spoiler +


private void btnPrime10001_Click(object sender, EventArgs e)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Int64 Total = 0;
List<int> list = new List<int>();
Boolean isPrime = true;
int i = 2;
while (list.Count() != 10001)
{
isPrime = true;
foreach (int j in list)
{
if (i % j == 0)
{
//Not a prime
isPrime = false;
break;
}
if (j > Math.Sqrt(i))
{
break;
}
} //j
if (isPrime)
{
list.Add(i);
}
i++;
} //i

stopWatch.Stop();
txtEuler.Text = list[10000].ToString() + " -- Time: " + stopWatch.Elapsed;
}

twitch.tv/cratonz
Prev 1 11 12 13 14 15 1032 Next
Please log in or register to reply.
Live Events Refresh
WardiTV Team League
12:00
Group B
WardiTV294
3DClanTV 12
IndyStarCraft 0
TKL 0
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko320
SortOf 170
ProTech28
TKL 25
IndyStarCraft 0
StarCraft: Brood War
Britney 39945
Sea 2164
Killer 803
Jaedong 720
BeSt 463
Rush 381
Mini 283
EffOrt 268
Larva 241
Soma 225
[ Show more ]
Stork 184
Light 176
Snow 171
ZerO 106
Sharp 99
hero 78
ToSsGirL 78
Sea.KH 48
Shine 45
Mind 45
Backho 40
[sc1f]eonzerg 27
Barracks 27
soO 23
Movie 18
Bale 17
Icarus 14
Terrorterran 11
Noble 10
Dota 2
XaKoH 496
XcaliburYe334
canceldota80
BananaSlamJamma49
Counter-Strike
fl0m1967
pashabiceps1499
Fnx 1397
shoxiejesuss710
x6flipin280
Super Smash Bros
Mew2King72
Westballz17
Other Games
singsing3330
B2W.Neo906
crisheroes280
Sick167
hiko118
ToD62
QueenE38
Trikslyr21
ZerO(Twitch)8
Organizations
Dota 2
PGL Dota 2 - Main Stream179
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• musti20045 14
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 7
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• lizZardDota290
League of Legends
• Jankos744
Upcoming Events
Big Brain Bouts
4h 49m
LetaleX vs Babymarine
Harstem vs GgMaChine
Clem vs Serral
Korean StarCraft League
14h 49m
RSL Revival
21h 49m
Maru vs Zoun
Cure vs ByuN
uThermal 2v2 Circuit
1d 2h
BSL
1d 7h
RSL Revival
1d 21h
herO vs MaxPax
Rogue vs TriGGeR
BSL
2 days
Replay Cast
2 days
Replay Cast
2 days
Afreeca Starleague
2 days
Sharp vs Scan
Rain vs Mong
[ Show More ]
Wardi Open
2 days
Monday Night Weeklies
3 days
Sparkling Tuna Cup
3 days
Afreeca Starleague
3 days
Soulkey vs Ample
JyJ vs sSak
Replay Cast
4 days
Afreeca Starleague
4 days
hero vs YSC
Larva vs Shine
Kung Fu Cup
4 days
Replay Cast
5 days
The PondCast
5 days
WardiTV Team League
5 days
Replay Cast
6 days
WardiTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-03-18
WardiTV Winter 2026
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Jeongseon Sooper Cup
BSL Season 22
CSL Elite League 2026
RSL Revival: Season 4
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

ASL Season 21
Acropolis #4 - TS6
2026 Changsha Offline CUP
CSL 2026 SPRING (S20)
CSL Season 20: Qualifier 1
Acropolis #4
IPSL Spring 2026
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
NationLESS Cup
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.