• What do you need help with? V. 3.0
    4,884 replies, posted
Can anyone help me with problem 3 from project euler, I currently have this : [code] bool IsPrime(int number) { bool isPrime = true; for(int i = 2;i < number;i++) { if(number % i == 0) { isPrime = false; break; } } return isPrime; } long long int Euler3(long long int number) { int i = 2; while(true) { if(number % i == 0) { if(IsPrime(number / i) && IsPrime(i)) { break; } } i++; } return number / i; } [/code] Link to the problem : [url]http://projecteuler.net/index.php?section=problems&id=3[/url] It currently spits out 8462696833 but its not the corrent answer and I have no idea why. Also I'm doing this to learn c++ so can anyone can give me some cc about my code ?
Well firstly you have to remember that a prime factor cannot be greater than the square root of the number you're finding it for (i.e. square root of 9 is 3, 4 is never going to work now is it?). So your IsPrime function can be greatly optimised. You could also simply do this: [cpp]bool IsPrime(long long number) { for(int i = 2;i < number;i++) { if(number % i == 0) return false; } return true; }[/cpp] Knowing this optimisation, you could also simplify your while loop (and work backwards). Like this: [cpp]for (long long i = (long long) sqrt((double) 600851475143); i > 2; i--)[/cpp] Removing the while loop. So your final solution would look something like this: [cpp]bool IsPrime(long long number) { for(int i = 2;i < number;i++) { if(number % i == 0) return false; } return true; } long long Problem3(long long number) { for (long long i = (long long) sqrt((double) number); i > 2; i--) { if(number % i == 0) { if(IsPrime(i)) { return i; } } } return 0; }[/cpp]
Wouldn't returning i just give you the smallest of solution of number and i ? Because the problem say you have to find the largest prime factor of the. also my new isPrime function : [code] bool IsPrime(int number) { for(int i = 2;i < (int) std::sqrt((double)number);i++) if(number % i == 0) return false; return true; } [/code]
If you look at the for loop I'm doing it goes down from the highest possible prime factor to the lowest, so returning i will return the highest. Be careful doing your loops like that without brackets, it can get messy if you don't watch out (but that's still much better).
Solved it : [code] bool IsPrime(int number) { for(int i = 2;i < (int) std::sqrt((double)number);i++) if(number % i == 0) return false; return true; } long long int Euler3(long long int number) { for(long long int i = (long long int) std::sqrt((double)number); i > 2; i--) { if(number % i == 0 && IsPrime(i)) { return i; } } return 0; } [/code] Also does anyone know why my implementation spit out such a big number?
In sfml, when you are using a specific colour for transparency (255, 0, 255) how do you stop the antialiasing blending to purple rather than what it should be blending to?
[QUOTE=Icedshot;30262724]In sfml, when you are using a specific colour for transparency (255, 0, 255) how do you stop the antialiasing blending to purple rather than what it should be blending to?[/QUOTE] If you're using a sprite/image, SFML does support the alpha channel
I'm making an tile based game which could in some ways resemble OpenTTD in terms of the players viewpoint, so basically I'm filling the screen(800*800) with tiles(28*15) which means that I at any given time have to draw around 1512 tiles to the screen, the way I do this is by drawing every individual tile onto the screen trough App->Draw(*localImage). But this gives me a framerate of 10! So I was wondering if there is a better way to draw tiles to a screen that saves a lot of power. Also note that localImage points to a common image so that each tile haven't got their own image.
[QUOTE=ief014;30262984]If you're using a sprite/image, SFML does support the alpha channel[/QUOTE] Im using bitmaps (which dont normally support alpha) and paint (which doesnt seem to either), and id rather know how to solve the issue (if there is a solution) than get around it I mean i could switch to pngs and use paint.net, but id prefer not to
[QUOTE=FPSMango;30263021]I'm making an tile based game which could in some ways resemble OpenTTD in terms of the players viewpoint, so basically I'm filling the screen(800*800) with tiles(28*15) which means that I at any given time have to draw around 1512 tiles to the screen, the way I do this is by drawing every individual tile onto the screen trough App->Draw(*localImage). But this gives me a framerate of 10! So I was wondering if there is a better way to draw tiles to a screen that saves a lot of power. Also note that localImage points to a common image so that each tile haven't got their own image.[/QUOTE] You should put all the tiles into one image, convert that image to a sprite, buffer it, and then draw the sprite. If you're using SFML 2 there's also RenderImage for off-screen rendering/buffering (see [url=http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderImage.php]here[/url]).
I seem to be following these SDL tutorials perfectly, and I understand every little bit they write, but I just can't seem to remember it. If you would ask me to recreate what I learned, I couldn't. Does anyone else has this? Should I just continue with the tutorials?
[QUOTE=Icedshot;30263146]Im using bitmaps (which dont normally support alpha) and paint (which doesnt seem to either), and id rather know how to solve the issue (if there is a solution) than get around it I mean i could switch to pngs and use paint.net, but id prefer not to[/QUOTE] If you're simply using a mask, I don't think you can. You're probably going to have to use images without antialiasing.
In Visual C++ 2010, is it possible to make all new files that I create follow a template? Like if I want to have a copyright notice at the top of every file can I make it do this automatically? Or do I just have to copy and paste each time?
I remember searching for a way to do it in C++ but I remember someone saying it was easy for C#, pretty sure it's on msdn. This may help: [url]http://msdn.microsoft.com/en-us/library/6db0hwky(v=vs.80).aspx[/url]
Can anyone help me with this error : fatal error LNK1169: one or more multiply defined symbols found I had all my stuff (practicing with project euler) in the main.cpp and I wanted to split it into more then one file so I made another file called euler.cpp and a file called quicksort.cpp and put my code in there now for some reason I get this linker error.
[QUOTE=Icedshot;30257770]Ok, what is happening is that drawing mytime in a large graphic style takes a certain amount of time, which means that the overall loop time is slightly longer than one second. So mytime gets increasingly far out from the real time You can fix it by finding the time it takes for the loop to complete, and adding that to mytime[/QUOTE] Awesome - it makes much more sense now. I should have noticed "large graphic style" in the pseudocode. :ohdear: Thanks both of you guys.
[QUOTE=TGiFallen;30212085]I seem to be having stack troubles with a garrysmod binary module. Note that ILuaInterface hides the return values so i have no way of know how many return values there actually are, i'm assuming no one will return 100 different values. [cpp] LUA_FUNCTION( ProfileFunction ) { ILuaInterface* g_Lua = Lua(); g_Lua->CheckType( 1 , GLua::TYPE_FUNCTION ); float time = 0; timer->Start(); g_Lua->Call(g_Lua->Top() - 1, -1 ); time = timer->Stop(); int i = 0; for (i = 100 ; 0 ; i--) { ILuaObject* retval = g_Lua->GetReturn(i); if (retval == NULL) { continue; } g_Lua->Push(retval); } g_Lua->Push(time); g_Lua->Push("Testing"); return g_Lua->Top(); } [/cpp] no matter where I push the time and string "testing" they still end up getting returned last, also this is my first cpp project, please be easy.[/QUOTE] please?
Alright, so I was able to get the bones in my armature working correctly, as seen in waywo: [img]http://i.cubeupload.com/v2bNl4.gif[/img] ^ Here I'm simply going through each bone and applying transformations via [b]GL.MultMatrix()[/b]. HOWEVER, I now need to get the actual transformed point, rather than just draw the point, so here's what I came up with: [csharp] public Bone Transform(String name, float frame, ref Matrix4 bar) { Bone[] bone_chain = root.GetBoneChain(name); for (int i = 0; i < bone_chain.Length - 1; i++) { Matrix4 translation_matrix = Matrix4.CreateTranslation(bone_chain[i].offset);// look into specification float[] channels = GetChannels(bone_chain[i].name, frame); for (int j = 0; j < bone_chain[i].channels.Length; j++) { float foo= channels[j]; switch (bone_chain[i].channels[j]) { /*case "Xposition": Matrix4 position_x= Matrix4.CreateTranslation(channels[j], 0.0f, 0.0f); GL.MultMatrix(ref position_x); break;*/ case "Yposition": Matrix4 position_y= Matrix4.CreateTranslation(0.0f, channels[j], 0.0f); bar= Matrix4.Mult(bar, position_y); break; /*case "Zposition": Matrix4 position_z= Matrix4.CreateTranslation(0.0f, 0.0f, channels[j]); GL.MultMatrix(ref position_z); break;*/ case "Xrotation": Matrix4 rotation_x= Matrix4.CreateRotationX(OpenTK.MathHelper.DegreesToRadians(channels[j])); bar= Matrix4.Mult(bar, rotation_x); break; case "Yrotation": Matrix4 rotation_y= Matrix4.CreateRotationY(OpenTK.MathHelper.DegreesToRadians(channels[j])); bar= Matrix4.Mult(bar, rotation_y); break; case "Zrotation": Matrix4 rotation_z= Matrix4.CreateRotationZ(OpenTK.MathHelper.DegreesToRadians(channels[j])); bar= Matrix4.Mult(bar, rotation_z); break; } } } return bone_chain[bone_chain.Length - 1]; } [/csharp] Note that wherever you see [b]bar= Matrix4.Mult(bar, transformation)[/b], it used to be [b]GL.MultMatrix(transformation)[/b]. After I run this, I apply the transformation (bar) to the points I'm drawing. My assumption was that this would the same as simply multiplying the OpenGL matrix by the transformations, but instead, this happens: [img]http://i.cubeupload.com/7HWlVM.gif[/img] SO, why is multiplying transformations to the OpenGL modelview matrix different than multiplying them to a Matrix4 matrix and then transforming points with it (Or is it different)? Where did I fuck up?
Shouldn't that be if (retval == NULL) break; or can something in the middle of the list be NULL too? Couldn't it be while ((retval = g_Lua->GetReturn(i)) != NULL) { g_Lua->Push...? [editline]6th June 2011[/editline] @TGiFallen
Usually these kind of errors mean you messed up the order of operations somewhere. If could be that you need to translate then rotate or that you should have reversed the matrix order when multiplying them.
Well the order seems to make sense when I think about whats going on, and in addition, the original animation (which is correct) is using the exact same order as the second. btw, this is how I draw the points after running Transform() (the function I just posted) on the bone in question. [csharp] GL.Begin(BeginMode.Lines); GL.Vertex3(Vector3.Transform(Vector3.Zero, bar)); GL.Vertex3(Vector3.Transform(tip.offset, bar)); GL.End(); [/csharp] Before the change, this was simply [csharp] GL.Begin(BeginMode.Lines); GL.Vertex3(Vector3.Zero); GL.Vertex3(tip.offset); GL.End(); [/csharp]
[QUOTE=esalaka;30268942]Shouldn't that be if (retval == NULL) break; or can something in the middle of the list be NULL too? Couldn't it be while ((retval = g_Lua->GetReturn(i)) != NULL) { g_Lua->Push...? [editline]6th June 2011[/editline] @TGiFallen[/QUOTE] No because g_Lua->GetReturn(int) returns them in reverse from what they should be
...What does NULL mean then? [editline]6th June 2011[/editline] In this context I'm not that silly~
OpenGL question, how are uniforms handled by OpenGL? do they stay tied to the shader until it's changed, do they get cleared only when you unbind the shader program (glUseProgram(0);), or do they get cleared every frame regardless of whether the shader was used or not? I'm trying to get some sort of abstraction done with OpenGL shaders, so that I don't have to pass my projection and modelview matrices to every renderable 3d object to pass it to the shader as a uniform like I'm doing now.
So I was looking for some game engines that use C#, and I found this one; [url]http://www.neoaxis.com/[/url] . It seems too good to be true, with all of its features and what it supports. Is it? Or is it actually as good as it seems? I don't want to waste my time on learning to use an engine that turns out to be more closed than I need, or is just flawed in some way. Has anyone here ever used/tried it?
[QUOTE=Mr. Smartass;30270100]So I was looking for some game engines that use C#, and I found this one; [url]http://www.neoaxis.com/[/url] . It seems too good to be true, with all of its features and what it supports. Is it? Or is it actually as good as it seems? I don't want to waste my time on learning to use an engine that turns out to be more closed than I need, or is just flawed in some way. Has anyone here ever used/tried it?[/QUOTE] Im sorry but is it really that hard to look in their website? Its free if your making a non commercial project, otherwise you have to pay As for their features [url]http://www.neoaxis.com/neoaxis/features[/url]
[QUOTE=Richy19;30270353]Im sorry but is it really that hard to look in their website? Its free if your making a non commercial project, otherwise you have to pay As for their features [url]http://www.neoaxis.com/neoaxis/features[/url][/QUOTE] I just noticed how stupid I'm being today, I'll stop posting. Also, thanks.
[QUOTE=TGiFallen;30269286]No because g_Lua->GetReturn(int) returns them in reverse from what they should be[/QUOTE] ILuaInterface::GetStackTop might work for getting the amount of returned values.
[QUOTE=raBBish;30270788]ILuaInterface::GetStackTop might work for getting the amount of returned values.[/QUOTE] well it turns out that ILuaInterface::Call(ILuaInterface::Top(),-1) actually does push the return values onto the stack, i just need to insert my return value at the top.
[QUOTE=robmaister12;30269903]OpenGL question, how are uniforms handled by OpenGL? do they stay tied to the shader until it's changed, do they get cleared only when you unbind the shader program (glUseProgram(0);), or do they get cleared every frame regardless of whether the shader was used or not? I'm trying to get some sort of abstraction done with OpenGL shaders, so that I don't have to pass my projection and modelview matrices to every renderable 3d object to pass it to the shader as a uniform like I'm doing now.[/QUOTE] One GL.Uniform* call isn't expensive enough that you would ned to worry about it. Just set the uniforms before you render and you're set. You should try to have as little rendering calls as possible. I don't think a few API calls per render would make that much of a difference. [editline]6th June 2011[/editline] [url]http://www.opengl.org/sdk/docs/man/xhtml/glUniform.xml[/url] You can scroll down to the description. I think it answers your question.
Sorry, you need to Log In to post a reply to this thread.