Just thought i'd post some thing on here since i haven't in a while. I wanted to start getting back into coding so i went to Project Euler. I randomly browsed some problems until I came across problem 21. Here is the problem:
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where ab, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
And this is the code i came up with to solve ( Notice: The code is not optimized! )
int sumimifier(int a) { int b = 1, count1 = 0, count2 = 0; while(b < a) { if(a%b == 0) { count1 += b; } b++; } b = 1; while(b < count1) { if(count1%b == 0) { count2 += b; } b++; } if(count2 == a && a != count1) { return(count1); } else { return 0; } }
int _tmain(int argc, _TCHAR* argv[]) { int a, b = 0;
for( a = 0; a < 10000; a++ ) { b += sumimifier( a ); }
printf( "%i", b ); getchar(); }
Thats basically it. If you have any better ways please post :)
Here is a quick AS3 random number explanation. In AS3, the Math.Random generates a random number between 0 and 1. So with that, we can get a random number between 0 and any number. Here is an example. Lets say we want a random number between 0 and 10. First we would need a dynamic textbox. Give it the instance name "displayRand". This would be our code:
This will now generate a random number. You can change the number after Math.random to anything depending on the range that you want. Post or email me any questions.
Here is a basic method of collision detection. First lets make 2 squares, make them both movieclips, and give he instance names square1 and square2. Now lets make it so that you can drag them around the stage, to make them overlap. Here is some basic code to do this:
var drag:Boolean = false; square1.addEventListener(MouseEvent.MOUSE_DOWN, mousedown); stage.addEventListener(MouseEvent.MOUSE_UP, mouseup); stage.addEventListener(Event.ENTER_FRAME, enterframe); function mousedown(e:MouseEvent):void{ drag = true; } function mouseup(e:MouseEvent):void{ drag = false; } function enterframe(e:Event):void{ if(drag == true){ square1.startDrag(); } if(drag == false){ square1.stopDrag(); } }
Now make a text box and make it dynamic and give it the instance name hittest.Now enter this code in the ENTER_FRAME function:
Hey guys, here is a quick addition to the code I gave you guys yesterday on AS3 arrow key movement. So today Im gonna show you guys how to make the object you are controlling wrap the stage. So here is the code I showed you yesterday:
Now we are going to make it so that when it goes off the right side of the stage, it will appear on the left. In the enter frame function, within the right arrow if stetment, put this code:
This makes it so that once the movieclip's x coordinate is off of the stage, It appears just off the left side of the screen. Now here is the enterframe function again with the wrap stage code inserted:
import flash.events.*; var rightArrow:Boolean; var leftArrow:Boolean; var upArrow:Boolean; var downArrow:Boolean; var speed:uint = 10; stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased); stage.addEventListener(Event.ENTER_FRAME, moveClip); function keyPressed(event:KeyboardEvent) { if (event.keyCode == Keyboard.RIGHT) { rightArrow = true; } if (event.keyCode == Keyboard.LEFT) { leftArrow = true; } if (event.keyCode == Keyboard.UP) { upArrow = true; } if (event.keyCode == Keyboard.DOWN) { downArrow = true; } } function keyReleased(event:KeyboardEvent) { if (event.keyCode == Keyboard.RIGHT) { rightArrow = false; } if (event.keyCode == Keyboard.LEFT) { leftArrow = false; } if (event.keyCode == Keyboard.UP) { upArrow = false; } if (event.keyCode == Keyboard.DOWN) { downArrow = false; } } function moveClip(event:Event) { if (rightArrow) { object_mc.x += speed; if (object_mc.x > stage.stageWidth) { object_mc.x = -(object_mc.width); } } if (leftArrow) { object_mc.x -= speed; if (object_mc.x < -(object_mc.width)) { object_mc.x = stage.stageWidth; } } if (upArrow) { object_mc.y -= speed; if (object_mc.y < -(object_mc.height)) { object_mc.y = stage.stageHeight; } } if (downArrow) { object_mc.y += speed; if (object_mc.y > stage.stageHeight) { object_mc.y = -(object_mc.height); } } }
This code all works basically the same as the first example. Post or email me any questions.
A handle to the process memory to be modified. The handle must have PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process.
lpBaseAddress [in]
A pointer to the base address in the specified process to which data is written. Before data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for write access, and if it is not accessible, the function fails.
lpBuffer [in]
A pointer to the buffer that contains data to be written in the address space of the specified process.
nSize [in]
The number of bytes to be written to the specified process.
lpNumberOfBytesWritten [out]
A pointer to a variable that receives the number of bytes transferred into the specified process. This parameter is optional. If lpNumberOfBytesWritten is NULL, the parameter is ignored.
So with that out of the way, I will show you a code snippit to writing memory to minesweeper.
int GetPID(LPCSTR ProcessName) //Function for getting the PID (Process ID) { HANDLE hProcessSnap; // Take a snapshot of all processes in the system. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
int _tmain(int argc, _TCHAR* argv[]) { if( IsActive( "winmine.exe" )) //Check if its running { HANDLE hProc; //Handle to Process unsigned long PID; //Variable to store the PID BYTE nop = { 0x90 }; //Buffer to write
PID = GetPID( "winmine.exe" ); //Obtains PID hProc = OpenProcess( PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, PID ); //Opens process to get the handle
//Change of 01002ff5 - ff 05 9c 57 00 01 - inc [0100579c] if( WriteProcessMemory( hProc, ( LPVOID )0x01002ff5, &nop, sizeof( nop ), 0 )) //Changing the opcode to 0x90 (hex for nop) { printf( "Time Successfully frozen!\n" ); } else { puts( "Could not write bytes for flags" ); }
Here is a quick tut on how to make a movieclip movable using the arrow keys. First we are gonna need to import some events:
Now lets declare a variable for speed:
Now a few variables which will tell us whether or not each button is pressed:
now we are going to add an event listener that listens for any key being pressed, one that checks for keys released and one for enter frame:
Now you need to create the movie clip that we are going to make move. It can be any shape or size. Give it the instance name object_mc . Now lets see the key down function. Type this:
This basically makes it so that when one of the variables is true, that button was pressed. Now for the key up function :
This makes it so that when the key is released, the variable returns to false. Without this, after pressing the button once, the movieclip would keep moving in that direction even after the key was released. Now make a movieclip. It can be any size or shape. Just give it the instance name "object_mc" without the quotes. Now for the enter frame function:
This actually moves the movieclip while the key is held. You can make it move faster or slower by changing the speed variable. So, as you can see arrow key movement is pretty easy with AS3. Any questions, email me or post.
I've seen lots of questions posted around many forums asking how to check if a process is running. And just to show you how easy it is, here is the code snippit.
See? Not very hard is it! The usage would just be something like: