Is there any penalty for wrapping a native callback with an anonymous delegate?
[code]public void NewClosure(ClosureDelegate func, int nfreevars = 0)
{
SQDLL.SQFUNCTION fn = delegate(IntPtr v)
{
GCHandle handle = GCHandle.FromIntPtr(SQDLL.sq_getforeignptr(v));
return func(handle.Target);
};
KeepAlive(fn);
SQDLL.sq_newclosure(m_VM, fn, nfreevars);
}[/code]
In java, i have one class in one file and then the main class in another.
Im compiling via javac *.java, tho I have also tried javac myClass.java mainClass.java as well as javac -classpath . mainClass.java
None on the classes are in a package
yet when I compile I get loads of
[code]mainClass.java:25: error: cannot find symbol
myclass.thisMethod();
^
symbol: method thisMethod()
location: variable myclass of type myClass<String>[/code]
Out of curiosity, what is it that you guys listen to when writing? I'm trying to get into C# but the silence is killing me. What do you guys use to provide a bit of ambiance?
[QUOTE=Corey_Faure;39361444]Out of curiosity, what is it that you guys listen to when writing? I'm trying to get into C# but the silence is killing me. What do you guys use to provide a bit of ambiance?[/QUOTE]
[video=youtube;llyiQ4I-mcQ]http://www.youtube.com/watch?v=llyiQ4I-mcQ[/video]
So I'm trying to implement the [URL="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#cite_note-sedgewick-12"]Sieve of Eratosthenes...[/URL]
Can anyone tell me why my program just hangs on the calculation? i.e. it just prints "calculating..." and sits there.
[cpp]#include <iostream>
using namespace std;
void findPrimeNumbers(int number) {
int n=0;
bool* boolArray = new bool[number]();
for(int i=0; i<number; i++) {
boolArray[i] = true;
}
for(int i = 2; i<sqrt(number); i++) {
cout << "calculating...[B]\n[/B]";
if(boolArray[i]) {
for(int j=(i^2+(n*i)); j<=number; n++)
boolArray[j] = false;
}
if(boolArray[i])
cout << i << "[B]\n[/B]";
}
}
int main()
{
findPrimeNumbers(13195);
system("pause");
return 0;
}[/cpp]
Thanks.
[editline]26th January 2013[/editline]
When I debug it, it's hanging on line 37.
[cpp]for(int j=(i^2+(n*i)); j<=number; n++)
boolArray[j] = false;[/cpp]
I think you need to reassign j to (i ^ 2 + n * i) each iteration. The first clause in a for is just evaluated once at the start.
[editline]25th January 2013[/editline]
You could do it like this:
[cpp]for (int j = i^2 + n*i; j <= number; j = i^2 + (++n*i))
boolArray[j] = false;[/cpp]
Hmm, it displays numbers now, but when I put in a number such as 55 it only goes up to 7.
[editline]26th January 2013[/editline]
Ok, turns out that I can't use ^ for power as that's the bitwise XOR, so I'm using std::pow(a,b) now. It works. But not really. See, I'm doing [url]http://projecteuler.net/problem=3[/url]
Now, when I execute the program for the example number, it gives me
5
7
13
29
35
65
91
Why do I get these last three? the first 3 are correct, I just need to eliminate the last three
[editline]26th January 2013[/editline]
Current code by the way:
[cpp]#include <iostream>
#include <cmath>
using namespace std;
void findPrimeNumbers(int number) {
int n=0;
bool* boolArray = new bool[number]();
for(int i=0; i<number; i++) {
boolArray[i] = true;
}
for(int i = 2; i<sqrt(number); i++) {
if(boolArray[i]) {
for (int j = pow(i,2) + n*i; j <= number; j = pow(i, 2) + (++n*i))
boolArray[j] = false;
}
if(boolArray[i] && number % i == 0)
cout << i << "\n";
}
return;
}
int main()
{
findPrimeNumbers(13195);
system("pause");
return 0;
}[/cpp]
well that's because the outer for loop only goes up to 7:
[cpp]for (int i = 2; i <= number; i++)
if(boolArray[i])
cout << i << "\n";[/cpp]
This was in response to why it wasn't printing all the numbers, and wow, with g++ the ^ operator was actually working as exponentiation.
Looks like you responded before you could read my edits :s
Word of warning here, your output time will decrease drastically if you remove the print from your loop.
Set it to a variable then print it out after performing the loop. (For debugging it's fine I suppose.)
I just got it to work by setting n = 0 at the end of each iteration of the i loop.
And another little optimization:
[cpp] for(int i = 2; i < sqrt(number); i++) {
int isq = i*i; // so we don't compute i squared every iteration of the inner loop
if(boolArray[i]) {
for(int j = isq + (n*i)); j<=number; j = isq + (++n*i))
{
boolArray[j] = false;
}
}
n = 0;
}[/cpp]
I'm debugging right now so I make sure it only displays prime factors :)
[editline]26th January 2013[/editline]
what the fuck how does that make it work
I don't know, I think it wasn't eliminating every multiple of the prime factors since n was continually growing. I was getting things like 27 and 49 as results before I added that.
Well guys, I'm glad that I got the jist of the operation down. This means I'm almost there to being able to make workable code from algorithms :) this is really big for me and I feel accomplished, thanks guys :)
[editline]26th January 2013[/editline]
Ok so now when I put in a stupid huge number (600851475143, that the problem calls for) I get this error
[quote]Unhandled exception at at 0x766C4B32 in Project Euler.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0027FC44.[/quote]
I presume this would be fixed by using a better sort of dynamic memory allocation process, like using std::vector?
[QUOTE=Meatpuppet;39362181]Well guys, I'm glad that I got the jist of the operation down. This means I'm almost there to being able to make workable code from algorithms :) this is really big for me and I feel accomplished, thanks guys :)
[editline]26th January 2013[/editline]
Ok so now when I put in a stupid huge number (600851475143, that the problem calls for) I get this error
I presume this would be fixed by using a better sort of dynamic memory allocation process, like using std::vector?[/QUOTE]
That number would require 40 bits to process. You're getting an overflow.
[U]log600851475143[/U] = 39.12
log2
Oh so I'll make 'number' a double (or more). Dumb mistake, thanks!
[editline]26th January 2013[/editline]
wait is that how you calculate memory usage?
[QUOTE=Meatpuppet;39362356]Oh so I'll make 'number' a double (or more). Dumb mistake, thanks!
[editline]26th January 2013[/editline]
wait is that how you calculate memory usage?[/QUOTE]
long long is 8 bytes.
Is there a way that I can use the inline buttons on my earbuds and my headphones with my laptop? My laptop has a headphone/mic in the same port, so I was thinking there is a way.
[QUOTE=Topgamer7;39362390]long long is 8 bytes.[/QUOTE]
Sounds like an assumption that'll break on x86_64.
[QUOTE=Jookia;39364337]Sounds like an assumption that'll break on x86_64.[/QUOTE]
The long long type is required to be at least 64 bits, and as far as I'm aware it is exactly 64 bits on x86_64.
Currently at GGJ 2013, and we're been stuck for hours trying to make a simple tile-based map work with SFML and Tiled, using a colission layer. I'm not exactly a code guy, but if anyone around has some idea on a simple way to get that working (without using tiled even) the team would be happy to send code and such.
[cpp]#include <iostream>
#include <cmath>
using namespace std;
int l = 0;
void findPrimeNumbers(long long number) {
int n=0;
bool* boolArray = new bool[number]();
for(int i=0; i<number; i++) {
boolArray[i] = true;
}
for(int i = 2; i<sqrt(number); i++) {
int isq = i*i;
if(boolArray[i]) {
for (int j = isq + n*i; j <= number; j = isq + (++n*i))
boolArray[j] = false;
}
if(boolArray[i] && (number % i == 0))
l = i;
n = 0;
}
return;
}
int main()
{
findPrimeNumbers(600851475143);
cout << l << "\n";
system("pause");
return 0;
}[/cpp]
This is still giving me a bad_alloc error.
[QUOTE=Meatpuppet;39366468][cpp]#include <iostream>
#include <cmath>
using namespace std;
int l = 0;
void findPrimeNumbers(long long number) {
int n=0;
bool* boolArray = new bool[number]();
for(int i=0; i<number; i++) {
boolArray[i] = true;
}
for(int i = 2; i<sqrt(number); i++) {
int isq = i*i;
if(boolArray[i]) {
for (int j = isq + n*i; j <= number; j = isq + (++n*i))
boolArray[j] = false;
}
if(boolArray[i] && (number % i == 0))
l = i;
n = 0;
}
return;
}
int main()
{
findPrimeNumbers(600851475143);
cout << l << "\n";
system("pause");
return 0;
}[/cpp]
This is still giving me a bad_alloc error.[/QUOTE]
Probably because you're asking it to allocate 600 billion bools :v:
Even if bools are just 1 bit each, that's over 75 GB
[QUOTE=account;39366508]Probably because you're asking it to allocate 600 billion bools :v:[/QUOTE]
How else would I implement the algorithm then?
[QUOTE=Meatpuppet;39366517]How else would I implement the algorithm then?[/QUOTE]
I'm not sure if the sieve of erastothenes is suited for this problem.
The big challenge here is to factor that number, so I would focus on that and then test primality later.
[QUOTE=account;39366604]I'm not sure if the sieve of erastothenes is suited for this problem.
The big challenge here is to factor that number, so I would focus on that and then test primality later.[/QUOTE]
I can factor it without using an array of bools.
[editline]26th January 2013[/editline]
So you're just saying I need to find another sieve?
[QUOTE=Meatpuppet;39366642]I can factor it without using an array of bools.
[editline]26th January 2013[/editline]
So you're just saying I need to find another sieve?[/QUOTE]
Well I just found the factors of that number in a few milliseconds by trial division. There aren't many, so you could just enter each one into the solution box and see if they work, it's kind of cheating, but hey, I'm sure a ton of people do it.
But also, the factors of the number are all small enough that you could use the sieve of erastosthenes to test them. Another cool (and pretty confusing, to me) way to test for primality is [url=http://en.wikipedia.org/wiki/Fermat%27s_little_theorem]Fermat's little theorem.[/url]
[QUOTE=account;39367228]Well I just found the factors of that number in a few milliseconds by trial division. There aren't many, so you could just enter each one into the solution box and see if they work, it's kind of cheating, but hey, I'm sure a ton of people do it.
But also, the factors of the number are all small enough that you could use the sieve of erastosthenes to test them. Another cool (and pretty confusing, to me) way to test for primality is [url=http://en.wikipedia.org/wiki/Fermat%27s_little_theorem]Fermat's little theorem.[/url][/QUOTE]
You mean just do a simple
[cpp]
for(int i = 0; i<=numer/2; i++) {
if(number % i == 0) cout << i << "\n";
}[/cpp]
and then just find the highest one that is prime?
[QUOTE=Meatpuppet;39367683]You mean just do a simple
[cpp]
for(int i = 0; i<=numer/2; i++) {
if(number % i == 0) cout << i << "\n";
}[/cpp]
and then just find the highest one that is prime?[/QUOTE]
Yeah, just go up from 3 to the square root of the number and increase i by 2 each time
I keep getting an invalid allocation size of 4 gigs. what
[editline]26th January 2013[/editline]
[cpp]void findPrimeNumbers(long long number) {
long *factors = new long[number/2]();
int n=0;
for(int i=0; i<number/2; i++) {
if(number % i == 0) {
factors[n] = i;
cout << factors[n] << "\n";
n++;
}
}[/cpp]
I'm just trying to get the factors first.
Sorry, you need to Log In to post a reply to this thread.