|
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. |
On October 29 2012 07:22 Fyodor wrote:Show nested quote +On October 29 2012 07:08 white_horse wrote:we're not allowed to use breaks or continues  I still don't understand how to store, say just 10 prime numbers in the vector. Well it will find all the primes unless you break out of the division test. You could make the loop index break the loop condition on purpose maybe? It wouldn't be an explicit break statement but it would achieve the same thing. cout << "Input the max range to search: "; int range; cin >> range;
vector<int> primes;
for(int i = 0; i < range; ++i) { for(int j = 2; j * j < i; ++j) { if(i % j == 0) { primes.push_back( j ); } } if (primes.size() >= 10) // breaks the loop after it finds 10 prime numbers. { i = range; } }
(only works if size() returns an int lol, not sure if it does)
Or just have the condition on the pushing also include a check that the size of the vector. You'll loop over the rest of the numbers, but considering you can't use break or continue, that doesn't seem to be a big deal.
if(i % j == 0 && primes.size() < 10) { primes.push_back( j ); }
|
I think size in stl always returns unsigned int
|
Yeah, I just looked it up. I don't program in c++ so don't know the api
|
I feel like somebody should mention that all the "improved" code doesn't find prime numbers, but instead finds factors of i that are smaller than sqrt(i) :p
Also, a much bigger optimization than replacing "j <= sqrt(i)" with "j*j <= i", would be only checking odd numbers and factors and treating 2 as a very simple special case.
|
1019 Posts
Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails.
I have this now, but it still doesn't work -_- what is wrong with it -______________-
+ Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
|
On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
Are you familiar with debugging ? you can run through your program step by step and see what happens to your variables. Dont see any really obvious mistake.
Edit1: Oh I think I see the problem. You keep checking for all primes after you found the Nprimes. You just dont store them. This may lead to very excessive processing times. I suggest using a while loop instead of the for loop and once you found the Nprimes or have reached a limit on i have the search stop. A less pretty way is to arbitrarily raise I after you found Nprimes.
Edit2: Another thing to note is that you keep checking if a number is a prime number after you have already have established it is not one. For example you can stop the check for large even numbers after it checks 2 (since this already shows its not a prime and thus further checks are useless). Again this is a very good spot for a while loop.
Overall your current implementation is very poor. If we assume all numbers of i are 1000000 instead of 1 to 1000000. it will take 1000000* 100000 computations to find your solution. while it should be way less if you remove all unnecessary computations.
|
You have a typo with your cin >> Nprimes instead of NPrimes.
|
On October 29 2012 12:20 Blisse wrote: You have a typo with your cin >> Nprimes instead of NPrimes.
I thought he actually had the code running. There goes my elaborate explanation on why it might not finish running / run fast.
One more optimization is only checking odd numbers after 2.
|
Does anyone here have experience with Soil, it's a C texture loading library?
I'm running into some really confusing problems. I've been trying to encapsulate it in a .h / .cpp file, which works fine in any test programs i create. However when i try to implement the exact same code into the program i need it in, i get:
Unhandled exception at 0x774615de in houses.exe: 0xC0000005: Access violation writing location 0xb037ac37.
As far as i can tell the problem occurs somewhere in these lines. + Show Spoiler + Texture[i] = SOIL_load_OGL_texture ( &chars[0], SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT );
Any thoughts or even guesses would be greatly appreciated.
edit: something i noticed, #include "SOIL.h" comes up underlined with the error cannot open source file. I have included SOIL.h and SOIL.c, in the source files. As well as set SOIL.lib as an additional dependancy under project settings. This doesn't cause a compile error and all the soil functions appear to be defined. Not sure why this is coming up now or how to fix it, or even if it is relevant.
|
Alright guys, I need some help with some browser testing.
I'm working on a website https://www.paydayloandebtassistance.com/test/
On Firefox, the tabs are working perfectly. The jQuery is doing what it needs too, but in Chrome, it's not working. Funny thing is, this is the second time I've used this code and the first time works flawlessly. No problems, but now, I'm having problems with them working on Chrome. It works on safari and on IE, just not chrome and you would think it would work on Chrome.
Here is the code:
$(document).ready(function(){ $(".triggers").click(function(){ $(".panels").toggle("fast"); $(this).toggleClass("active"); return false; }); });
$(document).ready(function(){ $(".trigger").click(function(){ $(".panel").toggle("fast"); $(this).toggleClass("active"); return false; }); });
The other code links to: http://jqueryjs.googlecode.com/files/jquery-1.3.2.js
The only thing I can think of is the link I'm using is outdated, but if it was outdate then why would it still work on the other site I have made with the same code?
I also have JS turned on in my Chrome settings.
$(document).ready(function(){ $(".triggers").click(function(){ alert("Thanks for visiting!"); }); });
Tested it with an alert and still the same thing, it's not working on chrome either.
Nevermind, tested it on other computers and it seems just to be my computer.
|
On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
I'm not sure if I understand you problem correctly but I don't understand what the first for of 1 million is for? Can't you just do a loop that execute "NPrimes" time and you do a square of each iteration?
|
I'd like to talk about std::vector in C++. Is it me or is it reaaaaally damn good? You mean I can add an arbitrary amount of elements in constant time AND have my fast random access? WTF where have you been all my life?
On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
I don't think you should declare your bool inside the for loop. Could be bad maybe?
vector <int> primes; bool isprime; for (...) isprime = true; etc
|
On October 30 2012 05:50 Fyodor wrote:I'd like to talk about std::vector in C++. Is it me or is it reaaaaally damn good? You mean I can add an arbitrary amount of elements in constant time AND have my fast random access? WTF where have you been all my life? Show nested quote +On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
I don't think you should declare your bool inside the for loop. Could be bad maybe? vector <int> primes; bool isprime; for (...) isprime = true; etc
It isn't bad, it's better. There's no performance hit and the scope of the variable's lifetime is limited.
std::vector is nice in that it has random element access and can grow. However it has annoying delayed destruction, and you have to use the vector swap trick to get the destruction to be run explicitly. Also growing is really slow as it requires an allocation, a copy, and a deallocation.
|
On October 30 2012 06:02 CecilSunkure wrote:Show nested quote +On October 30 2012 05:50 Fyodor wrote:I'd like to talk about std::vector in C++. Is it me or is it reaaaaally damn good? You mean I can add an arbitrary amount of elements in constant time AND have my fast random access? WTF where have you been all my life? On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
I don't think you should declare your bool inside the for loop. Could be bad maybe? vector <int> primes; bool isprime; for (...) isprime = true; etc
It isn't bad, it's better. There's no performance hit and the scope of the variable's lifetime is limited. std::vector is nice in that it has random element access and can grow. However it has annoying delayed destruction, and you have to use the vector swap trick to get the destruction to be run explicitly. Also growing is really slow as it requires an allocation, a copy, and a deallocation. huh? I read everywhere that push_back() takes constant time on average.
Thx for the tip on declaring within the loop though, that's pretty cool. Does it also work with C# and Java?
|
On October 30 2012 06:23 Fyodor wrote:Show nested quote +On October 30 2012 06:02 CecilSunkure wrote:On October 30 2012 05:50 Fyodor wrote:I'd like to talk about std::vector in C++. Is it me or is it reaaaaally damn good? You mean I can add an arbitrary amount of elements in constant time AND have my fast random access? WTF where have you been all my life? On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
I don't think you should declare your bool inside the for loop. Could be bad maybe? vector <int> primes; bool isprime; for (...) isprime = true; etc
It isn't bad, it's better. There's no performance hit and the scope of the variable's lifetime is limited. std::vector is nice in that it has random element access and can grow. However it has annoying delayed destruction, and you have to use the vector swap trick to get the destruction to be run explicitly. Also growing is really slow as it requires an allocation, a copy, and a deallocation. huh? I read everywhere that push_back() takes constant time on average. Thx for the tip on declaring within the loop though, that's pretty cool. Does it also work with C# and Java? Don't know about other languages. Push back has constant time unless there's a resize involved.
|
This code works. It's best to write a function to test if a number is a prime number like I did. It isn't good code performance wise if you want to test large ranges of numbers.
+ Show Spoiler +unsigned int NPrimes; cin >> NPrimes;
vector <int> primes;
int currentNumber = 2;
do { if (IsPrimeInt(currentNumber)) primes.push_back(currentNumber);
++currentNumber;
}while (primes.size() < NPrimes);
The function to test if a number is a prime: bool IsPrimeInt(int number) { if (number <= 1) return false; else if(number == 2) return true; //PRELIMINARY CHECK TO TEST IF THE NUMBER = 2. else if(number % 2 == 0) return false; //QUICK CHECK TO TEST IF THE NUMBER IS EVEN
for (int i = 3; i < number/2; i+=2) //THIS CODE WILL ONLY CHECK ODD NUMBERS { if (number%i == 0) return false; } return true; }
I've got some code that I copied from a projecteuler.net exercise thread. This code is very very performant but isn't suitable for your exercise. I don't know how to make the 2000000 bitset to a variable. It needs to be a constant. + It uses continues which weren't allowed.
+ Show Spoiler +#include <bitset>
vector <int> primes;
bitset<2000000> Sieve; __int64 sum = 0;
Sieve.flip(); // Set all bits to 1 Sieve[0].flip(); // Set 0 and 1 to not prime Sieve[1].flip();
// Check all nos from 2 to 1 million for(long i = 2; i < 2000000; ++i) { if(!Sieve[i])continue; // If marked not prime else // return to head of loop // Set all multiples as not prime for(long j = 2*i; j < 2000000; j += i) { Sieve[j] = 0; } }
for(long i = 2; i < 2000000; ++i) { if(Sieve[i])primes.push_back(i); }
|
Wanted to share http://www.codecademy.com/ with anyone that wants to learn python. There is courses for JavaScript, Ruby, HTML and CSS and JQuery as well! You can log in with your facebook/Google+/Twitter. It's an interactive site so no downloads is requiered. They have small "achievements" as well to keep it fun! ^^
|
On October 30 2012 07:14 CecilSunkure wrote:Show nested quote +On October 30 2012 06:23 Fyodor wrote:On October 30 2012 06:02 CecilSunkure wrote:On October 30 2012 05:50 Fyodor wrote:I'd like to talk about std::vector in C++. Is it me or is it reaaaaally damn good? You mean I can add an arbitrary amount of elements in constant time AND have my fast random access? WTF where have you been all my life? On October 29 2012 11:52 white_horse wrote:Thank you all for the input. You guys are a lot more helpful than my GSIs. They don't really respond to emails. I have this now, but it still doesn't work -_- what is wrong with it -______________- + Show Spoiler +
int NPrimes; cin >> Nprimes;
vector <int> primes;
for (int i = 0; i < 1000000; i++) { bool isprime = true; for (int j = 2; j <= sqrt(i); j++) { if (i%j == 0) { isprime = false; } }
if ((isprime == true) && (primes.size() < NPrimes)) { primes.push_back(i); }
}
I don't think you should declare your bool inside the for loop. Could be bad maybe? vector <int> primes; bool isprime; for (...) isprime = true; etc
It isn't bad, it's better. There's no performance hit and the scope of the variable's lifetime is limited. std::vector is nice in that it has random element access and can grow. However it has annoying delayed destruction, and you have to use the vector swap trick to get the destruction to be run explicitly. Also growing is really slow as it requires an allocation, a copy, and a deallocation. huh? I read everywhere that push_back() takes constant time on average. Thx for the tip on declaring within the loop though, that's pretty cool. Does it also work with C# and Java? Don't know about other languages. Push back has constant time unless there's a resize involved.
std::vector is a simple wrapper around a vanilla array with resize semantics added for you. All elements are guarenteed to be contigious in memory starting at &v[0] where v is an instance of std::vector.
Sometimes you can avoid the resize cost by using std::vector:: reserve.
|
Hi guys. I am interested in learning programming from scratch with an intention of making it my main job. I was advised that Android Developer is good destination point by a friend. So my questions: 1) How does one get started to become an Android Developer? Any guide for a step-by-step plan of studying or online courses you can advise? 2) Would you advise becoming an Android Developer yourself?
|
On November 01 2012 04:26 Cheerio wrote: Hi guys. I am interested in learning programming from scratch with an intention of making it my main job. I was advised that Android Developer is good destination point by a friend. So my questions: 1) How does one get started to become an Android Developer? Any guide for a step-by-step plan of studying or online courses you can advise? 2) Would you advise becoming an Android Developer yourself? I think you're getting a little too far ahead of yourself. From the way you worded it, it seems that you haven't even learned or tried out programming yet and you want to decide to make it your job? Overall I think its a bad way to approach it. Try it out first and if you enjoy it then you can start to take it more seriously and make a career out of it.
|
|
|
|