Aeternus
Senior Members-
Posts
349 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Everything posted by Aeternus
-
Ouch, bit harsh don't you think? I mean, don't get me wrong, various Windows versions (most if not all annoy me no end in a lot of ways (as do a lot of other OSs), but I doubt you'll find a perfect OS and I'd say most Windows OS are at least low quality ;P
-
Heh, makes no sense? It should work as advertised, although the lack of registers in x86 assembly becomes a pain :\ It just does what I suggested above for output and for input it just takes what's been read in and converts each character from input to an integer and adds it to the input integer, making sure to times the input by 10 at each stage so it is of the right order (ie 567 goes 5 -> 50 + 6 -> 560 + 7 etc). The reason for this is so that it can do more than 9! (multiple digits). It could have been commented better I admit but I didn't spend that long on it Sure, you can take the argument in from the command line as well, I just chose to read it in from stdin (although I didn't bother to do any checks so random data will probably kill it). If you have a question about something in particular, feel free to ask. Java doesn't accept inline assembly afaik since it compiles to byte code so it can run on any machine. Also I think the idea behind Atheists challenge (as I think he suggested in the thread) was to be able to calculate factorials larger than would fit in a single integer or long although that is only the impression I got.
-
I'm extremely confused as to what your point is here. Linux & Co offer a standard generic linux kernel which people can use but they also offer the source as well so that you are free to change it and redistribute your changes as you see fit so as to provide a completely different kernel or you can even start distributing the same kernel. This is completely different to what you have with Microsoft whereby the kernel source is never revealed (unless you are in one of their partnership programs and then only select parts) and if you try to redistribute their kernel, you WILL be sued. You make it seem like everyone only ever uses the stock kernel but this isn't the case, almost all linux distributions have their own kernel patches which they apply to each new version of the stock kernel and then distribute to their users. The open source nature means if people do see a problem they can create a fork of the code as has been mentioned and people can join them if they wish. The point you are making about Linus & Co "controlling" what goes into the stock kernel is true to some extent(*) but not in the same way as MS and it's completely irrelevant in reality as, as I just pointed out, the open source nature of the kernel means that if they won't include it in the stock kernel, you are free to modify the kernel anyways and use AND distribute your changes so that anyone can benefit from them. Linux & Co don't act as a monopolistic gatekeeper (as anyone can do exactly what they are doing), they are simply a major trusted entity (amongst many kernel distributors). * This is true in the context that if you are a developer and you wish to reach a great deal of people *easily* through distribution via the stock kernel then yes, your code is subject to scrutiny by the kernel group and yes for some people this can cause anguish and is an interesting issue (similar to the issue of social pressure vs authoritative control in society) but as mentioned above, the fact that you are free to distribute your own kernel, or patches to be applied to the stock kernel to others means that there is no real restriction implied here in terms of source, only in terms of someone else doing the distribution for you. I'm sorry if this doesn't come across well, and I admit there are parallels to be drawn between the effects of control on distribution of one of many trusted kernels (albeit usually the daddy) and the complete authoritative control of source,workings, distribution etc (everything) but the two are clearly not the same and to suggest that because you can note some (practically different) elements of control within certain aspects of the linux community, that this means that the entire process is as controlled and restricted as what occurs with the Windows Kernel seems to me to be a little bit wrong :\ Perhaps you are not suggesting that they are the same and are only pointing out the lesser "sense" of control that has been mentioned but if so it didn't come across that way. [Edit] Bah, didn't realise, I'm reiterating 1veedo here, oh well.
-
factorial.asm .section .data answerText: .ascii "The answer = " answerTextLen: .byte 13 .section .bss .comm input 25 .comm answer 25 .section .text .globl _start _start: /* Read in */ movl $0,%ebx movl $input,%ecx movl $25,%edx movl $3,%eax int $0x80 /* Length comes back on eax */ /* Convert to Integer - %eax = accum, %ecx = input+length, %ebx = input, %edx = digit */ movl $input, %ebx movl %ebx, %ecx addl %eax, %ecx /* Move length into ecx */ subl $2, %ecx movl $0, %eax cl : cmp %ebx, %ecx jb cend /* Finished reading input? */ movl $10, %edx mul %edx movb (%ebx), %dl subl $48, %edx addl %edx, %eax addl $1, %ebx jmp cl cend : pushl %eax /* Push input onto stack */ /* Write (syscall 4) answerText to stdout (fd 1) */ movl $1,%ebx movl $answerText,%ecx movl $0, %edx movb (answerTextLen),%dl movl $4,%eax int $0x80 /* Calculates 10 factorial */ popl %eax /* Get Input back again */ movl %eax, %ebx movl $1, %eax movl $0, %ecx fl : cmp %ecx, %ebx jbe end /* Finish if neccessary */ mul %ebx subl $1, %ebx jmp fl end : /* Convert number to string - val = eax, 10 = ebx, temp = edx, answer = ecx */ movl $10, %ebx movl $answer, %ecx addl $25, %ecx /* Add New Line */ subl $1, %ecx movb $10, (%ecx) cdq loop : idiv %ebx /* eax = eax / ebx ; edx = eax % ebx */ addl $48, %edx subl $1, %ecx movb %dl, (%ecx) movl $0, %edx cmpl %eax, %edx jb loop /* Print Result */ movl $answer,%edx addl $25, %edx subl %ecx, %edx movl $1,%ebx movl $4,%eax int $0x80 /* Exit */ movl $1,%eax movl $0,%ebx int $0x80 Makefile factorial : factorial.o ld -s -o factorial factorial.o factorial.o : factorial.asm as factorial.asm -o factorial.o Something like that is what I was suggesting. You can see in the input and output sections, the conversions from and to integers and strings.
-
Since you are only printing positive integers, you can quite easily convert that integer result to a base 10 string by having a large array/block predeclared (this is easier as you know you won't be able to do very large factorials, so there's a limit on the space you'll need to print it) and then using the following concept - result = ? temp = 0 string = "" while result > 0 do temp = result % 10 string = (char)(temp + 48) ++ string result = result / 10 // integer division od That's just some pseudocode but you can see the idea, take the last digit put it in the string (think ascii), then chop it off by division until you've got all the digits. Clearly it's more complicated than that in assembly, but you can easily declare a block of memory in the .data pr .bss section or whatever and an integer length field and do the same thing. Then you can use the write syscall to print it out - http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html (useful list of syscalls) . It's early in the morning so I can't really offer more than this atm but I might knock together a simple example later on.
-
what's a good program for a noob to 'hack'? preffereably python.
Aeternus replied to Dak's topic in Computer Science
Just about to go to bed, but I figure I might as well list a small python project a friend maintains which might suit your needs - http://andrewprice.me.uk/projects/pybackpack/ . -
You can just click plain text at the bottom or https://aeternus.no-ip.org/factorial-trac/trac.cgi/browser/Math.java?rev=20&format=txt . You are correct, I didn't think of that Sorry it's a little messy at the moment (comments etc all over) but I haven't had a chance to remove them.
-
https://aeternus.no-ip.org/factorial-trac/trac.cgi/browser/Math.java?rev=20 Some improvements and some changes that may be for the worse in terms of range. I looked at the multiplication and decided to use the Karatsuba method of multiplication to speed things up a bit. This is fairly simple compared to some of the multiplication algorithms (such as Schonhage-Strassen) but most of the others seemed to be FFT algorithms which I don't know enough about to begin implementing them. This doubled the speed (to the effect that it now calculates 1000000! in around 1 hour 20 minutes instead of 2 hours 40) but means that, given my implementation, it takes quite a lot of memory compared to the previous implementation (just over 1GB was used when calculating 1000000!). I'm not sure how much of this was garbage collection issues etc but I imagine that a lot of it was my poor implementation of things. I plan eventually to look into improving this some more (especially with the neatness and overall correctness), perhaps by looking at the other algorithms but I have a lot of reading to do before that and I'll probably implement some kind of JUnit tests or try to prove the correctness of some of the building block operations.
-
http://pastie.caboo.se/70939 New Version which grows the array when needed and does the calculation in a more tree like fashion to improve the speed a little (so you are more often multiplying small numbers than big which are easier to compute with).
-
They are talking about the speed to render a page. A page with lots of styles, images and mainly javascript etc can take a while to render even after it has been downloaded (even if it is usually only a second or so). During their tests they chuck huge bloated pages (for instance there are javascript tests that just dynamically create huge amounts of html objects and place them on the page) at the browsers and see which one finishes everything first.
-
Just realised, I forgot to change padWithZeroes to pad it out to 9 digits when I changed to base 2 billion. I'll add a new fixed version soon with some more comments. Changing down to base 1 billion and just padding the digits is far quicker as my original base conversion was a bit slow. Atheist, while I understand what you are saying and am aware of the fact that you can hack around the lack of signed integers (I used the same hack in my original code with bytes and I also had to do this when doing image manipulation stuff for one of my graphics modules), I think 4*10^9 would be too large as during the multiplication I will have to store the result of the multiplication of 2 digits , which I won't be able to store in a *signed* long with that large a number (ie potentially (4*10^9 - 1) * (4 * 10^9 -1) = ~16 * 10^18 whereas the max long is ~9 * 10^18). That along with the fact that conversion to a string at the end is easier with base 1 * 10^9 makes me think that uping to something like base 3 * 10 ^ 9 which might fit, would be less rewarding. I'm looking into speeding it up in other ways and am keeping your comments in mind [edit] http://pastie.caboo.se/69641 Here is the new code. It is rather a bit faster than the previous but I can still think of a few potential improvements (as I'm sure you are suggesting there are). It now manages 30000! in 6s on my desktop and 50000! in 20s. I'll try making it a little more robust in a little while by having an increasing size array (which will double or similar in size when it reaches it's make to avoid too many problems), which may slow it down a bit but might open the doors for a more interesting approach than a for loop, as well as reducing problems as given that it now calculates much larger numbers in a much smaller time, the increasing small arbitrary limit of 200000 digits (although in base 10^9) could become problematic (especially as it isn't error checked at present).
-
class Math { private static final long NON_BIGNUM_LIMIT = (long)2000000000 * (long)2000000000; public static String factorial( String value ) { BigNatural a = new BigNatural("1"); BigNatural b = new BigNatural(value); int val = Integer.parseInt(value); long currentValue = 1; while(val >= 2) { while (((NON_BIGNUM_LIMIT / currentValue) > val) && (val >= 2)) { currentValue *= val--; } b.set(currentValue); a.multiply(b); currentValue = 1; //b.decrement(); //val--; } return a.toString(); } public static void main ( String args[] ) { System.out.println(factorial(args[0])); } static public class BigNatural { // We store the values as a base255 number (we use 255 to represent the end of the list for speed of calculation) public int base = 10; public static final int DEFAULT_BASE = 2000000000; public static final int MAX_LENGTH = 200000; private int[] value; // Used for intermediate calculations and we don't // want them remade every time as that would be rather // slow. private int[] finalResult; public BigNatural(String strVal) { this(strVal,DEFAULT_BASE); } public BigNatural (String strVal, int base) { //int val = Integer.parseInt(strVal); // Clearly we won't be able to compute more than the range of int factorial value = new int[MAX_LENGTH]; this.base = base; finalResult = new int[MAX_LENGTH]; // Init value[0] = 0; finalResult[0] = 0; finalResult[1] = -1; // Set value set(strVal); } /** * Decrements by 1 */ public void decrement() { long intVal = value[0]; // No unsigned bytes :'( int i = 0; while (intVal == 0) { intVal = value[++i]; } value[i--]--; while(i >= 0){ value[i] = (base - 1); i--; } //value[0] = (byte) intVal; } /** * Sets the current number to a new number */ public void set( String strVal ) { // Set value set(Long.parseLong(strVal)); } public void set(long val) { int i = 0, j = 0; long pow = 1; while ((val / pow) >= 1) { pow *= base; i++; } //System.out.println("Power " + pow + " val " + val); j = i; while(i >= 0) { value[i--] = (int)(val / pow); val = val % pow; pow /= base; } value[j] = -1; } /** * Converts to a denary string */ public String toString() { BigNatural power = new BigNatural(new String("" + base),1000000); //BigNatural powerRaiser = new BigNatural(new String("" + base),(byte)10); BigNatural tmpValue = new BigNatural("0",1000000); BigNatural powerUp = new BigNatural("1", 1000000); BigNatural newValue = new BigNatural("0", 1000000); // Iterate over digits int i = 0; while((value[i] >= 0)) { tmpValue.set(value[i]); tmpValue.multiply(powerUp); newValue.add(tmpValue); powerUp.multiply(power); i++; } int[] bytes = newValue.getBytes(); i = 0; while(bytes[i++] >=0); i--; i--; StringBuilder x = new StringBuilder(i); if (i >= 0) x.append(bytes[i--]); while(i>=0) { x.append(padWithZeroes(bytes[i--],6)); } return x.toString(); } public static String padWithZeroes(int x, int len) { String xs = new String("" + x); StringBuilder g = new StringBuilder(); for(int i = xs.length(); i < len; i++) { g.append('0'); } g.append(xs); return g.toString(); } public void printString() { int i = 0; while(value[i++] >= 0); i--;i--; while(i >= 0) { System.out.print(value[i--]); } System.out.println(); } /** * Multiplies this big number with the other and stores the value in this * one . * * This stores the result in this object sine I wish to avoid creating * lots of objects over and over as this will be horribly inefficient * and garbage collection will probably not be able to keep up. * * This basically does long multiplication in base */ public void multiply(BigNatural other) { int[] otherBytes = other.getBytes(); int i = 0; int j = 0; long intVal = 0; long overflow = 0; finalResult[0] = 0 ; finalResult[1] = -1; // Iterate over a's digits while(value[i] >= 0) { // Multiply by every b digit j = 0; overflow = 0; while(otherBytes[j] >= 0) { if (finalResult[j+i] < 0) { finalResult[j+i] = 0; finalResult[j+i+1] = -1; } intVal = (long)finalResult[j+i] + ((long)value[i] * (long)otherBytes[j]) + overflow; overflow = intVal / base; intVal = intVal % base; finalResult[j+i] = (int)intVal; j++; } while (overflow > 0) { if (finalResult[j+i] < 0) { finalResult[j+i] = 0; finalResult[j+i+1] = -1; } intVal = finalResult[j+i] + overflow; overflow = intVal / base; intVal = intVal % base; finalResult[j+i] = (int)intVal; j++; } value[i] = finalResult[i]; i++; } // Put back in value while (finalResult[i] >= 0) { value[i] = finalResult[i++]; } value[i] = -1; } private int[] getBytes() { return value; } /** * Adds two raw big naturals together */ public void add(BigNatural bigB) { int[] a = value; int[] b = bigB.getBytes(); long overflow = 0; long intVal = 0; int i = 0; while((b[i] >= 0)) { if (a[i] < 0) { a[i] = 0; a[i+1] = -1; } intVal = a[i] + b[i] + overflow; overflow = intVal / base ; intVal = intVal % base; a[i] = (int)intVal; i++; } while(overflow > 0) { if (a[i] < 0) { a[i] = 0; a[i+1] = -1; } if (b[i] < 0) { b[i] = 0;b[i+1] = -1; } intVal = a[i] + b[i] + overflow; overflow = intVal / base ; intVal = intVal % base; a[i] = (int)intVal; i++; } } } } Slight improvement, that avoids using the BigNatural class as much as possible. Doesn't really make a huge difference by tends to knock off around about 20% of the time. [Edit] Didn't realise, it should have been using base 2 billion, not 2 million That sped things up a little bit.
-
class Math { public static String factorial( String value ) { BigNatural a = new BigNatural("1"); BigNatural b = new BigNatural(value); int val = Integer.parseInt(value); while(val >= 2) { a.multiply(b); b.decrement(); val--; } return a.toString(); } public static void main ( String args[] ) { System.out.println(factorial(args[0])); } static public class BigNatural { // We store the values as a base255 number (we use 255 to represent the end of the list for speed of calculation) public int base = 10; public static final int DEFAULT_BASE = 2000000; public static final int MAX_LENGTH = 200000; private int[] value; // Used for intermediate calculations and we don't // want them remade every time as that would be rather // slow. private int[] finalResult; public BigNatural(String strVal) { this(strVal,DEFAULT_BASE); } public BigNatural (String strVal, int base) { //int val = Integer.parseInt(strVal); // Clearly we won't be able to compute more than the range of int factorial value = new int[MAX_LENGTH]; this.base = base; finalResult = new int[MAX_LENGTH]; // Init value[0] = 0; finalResult[0] = 0; finalResult[1] = -1; // Set value set(strVal); } /** * Decrements by 1 */ public void decrement() { long intVal = value[0]; // No unsigned bytes :'( int i = 0; while (intVal == 0) { intVal = value[++i]; } value[i--]--; while(i >= 0){ value[i] = (base - 1); i--; } //value[0] = (byte) intVal; } /** * Sets the current number to a new number */ public void set( String strVal ) { // Set value set(Integer.parseInt(strVal)); } public void set(long val) { int i = 0, j = 0; long pow = 1; while ((val / pow) >= 1) { pow *= base; i++; } j = i; while(i >= 0) { value[i--] = (int)(val / pow); val = val % pow; pow /= base; } value[j] = -1; } /** * Converts to a denary string */ public String toString() { BigNatural power = new BigNatural(new String("" + base),1000000); //BigNatural powerRaiser = new BigNatural(new String("" + base),(byte)10); BigNatural tmpValue = new BigNatural("0",1000000); BigNatural powerUp = new BigNatural("1", 1000000); BigNatural newValue = new BigNatural("0", 1000000); // Iterate over digits int i = 0; while((value[i] >= 0)) { tmpValue.set(value[i]); tmpValue.multiply(powerUp); newValue.add(tmpValue); powerUp.multiply(power); i++; } int[] bytes = newValue.getBytes(); i = 0; while(bytes[i++] >=0); i--; i--; StringBuilder x = new StringBuilder(i); if (i >= 0) x.append(bytes[i--]); while(i>=0) { x.append(padWithZeroes(bytes[i--],6)); } return x.toString(); } public static String padWithZeroes(int x, int len) { String xs = new String("" + x); StringBuilder g = new StringBuilder(); for(int i = xs.length(); i < len; i++) { g.append('0'); } g.append(xs); return g.toString(); } public void printString() { int i = 0; while(value[i++] >= 0); i--;i--; while(i >= 0) { System.out.print(value[i--]); } System.out.println(); } /** * Multiplies this big number with the other and stores the value in this * one . * * This stores the result in this object sine I wish to avoid creating * lots of objects over and over as this will be horribly inefficient * and garbage collection will probably not be able to keep up. * * This basically does long multiplication in base */ public void multiply(BigNatural other) { int[] otherBytes = other.getBytes(); int i = 0; int j = 0; long intVal = 0; long overflow = 0; finalResult[0] = 0 ; finalResult[1] = -1; // Iterate over a's digits while(value[i] >= 0) { // Multiply by every b digit j = 0; overflow = 0; while(otherBytes[j] >= 0) { if (finalResult[j+i] < 0) { finalResult[j+i] = 0; finalResult[j+i+1] = -1; } intVal = (long)finalResult[j+i] + ((long)value[i] * (long)otherBytes[j]) + overflow; overflow = intVal / base; intVal = intVal % base; finalResult[j+i] = (int)intVal; j++; } while (overflow > 0) { if (finalResult[j+i] < 0) { finalResult[j+i] = 0; finalResult[j+i+1] = -1; } intVal = finalResult[j+i] + overflow; overflow = intVal / base; intVal = intVal % base; finalResult[j+i] = (int)intVal; j++; } value[i] = finalResult[i]; i++; } // Put back in value while (finalResult[i] >= 0) { value[i] = finalResult[i++]; } value[i] = -1; } private int[] getBytes() { return value; } /** * Adds two raw big naturals together */ public void add(BigNatural bigB) { int[] a = value; int[] b = bigB.getBytes(); long overflow = 0; long intVal = 0; int i = 0; while((b[i] >= 0)) { if (a[i] < 0) { a[i] = 0; a[i+1] = -1; } intVal = a[i] + b[i] + overflow; overflow = intVal / base ; intVal = intVal % base; a[i] = (int)intVal; i++; } while(overflow > 0) { if (a[i] < 0) { a[i] = 0; a[i+1] = -1; } if (b[i] < 0) { b[i] = 0;b[i+1] = -1; } intVal = a[i] + b[i] + overflow; overflow = intVal / base ; intVal = intVal % base; a[i] = (int)intVal; i++; } } } } There's the new revised edition. I may get around to commenting it and tidying things up a bit more, depending on whether I can be bothered etc. It isn't as fast as using other precision number classes but it works well enough. The conversion from base 2 billion to 10 is a bit iffy, it is a lot faster now I realised that you can convert to base 1 billion and pad digits with 0s. It does 10000! on my macbook (1.83Ghz Core Duo) in around 14-15s but takes around 1 minutes and a bit to calculate 20000! so you can see that the complexity isn't nice and it won't be winning any awards any time soon On my desktop (2.4Ghz Core 2 Duo) it manages 20000! in 27s and 30000! in about the same time as the my macbook handles 20000! which is better still I don't think it'll be beating Erlang any time soon, heh.
-
#include<stdio.h> #include<math.h> int main(int argc, char* args[]) { /* GO GO YT CODE! */ int A, B; float C; int value = 1000000; if (argc > 1) { sscanf(args[1], "%d", &value); } for (A = 2; A <= value; A++) { C = sqrt((float)A); for(B=2; B <= C; B++) { if ((A % B) == 0) goto endofloop; } printf("%d\n", A); endofloop : ; } } Here's a simple C adaptation of YTs code, without 1 as a prime
-
sieve :: [integer] -> [integer] sieve [] = [] sieve (h:t) = h : (sieve [x | x <- t, x `mod` h /= 0]) primes = sieve [2..] That's the sieve of eratosthenes in haskell You can just type primes and it'll keep generating primes as long as it doesn't run out of memory/stack space. It's not amazingly fast but it should do 65000 reasonably quickly. Here is some rather old and extremely crappy code a few years ago when I was first learning bits of C++. The style is crap and it's horribly unreadable, and probably bug ridden but it works for the most part - prime.cpp // Primes.cpp : Defines the entry point for the console application. // // Include the include // Basic Headers #include "stdafx.h" // Basic Variable Declaration unsigned long int num; unsigned long int temp; // Value for use as highest prime (before init) unsigned long int maxsize; bool found; char numb[20]; char tchar[2]; float sqr; long int pos; short int add; bool check; char path[500]; unsigned long int fileplace; bool dump; // Value to check whether an eop dump is needed long int test; // Remember later, for increase +1 // Main Code int main(int argc, char* argv[]) { // Ask for File cout << "Please enter the path of the file to save to"; cin >> path; // Set File Pointers ofstream ofileptr(path,ios::app); ifstream ifileptr(path,ios::in); // Initialise Fileplace fileplace = 0; temp = 0; // Read in the Number to go up to cout << "Please Enter a Number to go up to \n"; cin >> num; // Read in the memory to use cout << "Please enter the number of primes you would like to store in ram"; cin >> maxsize; // Create new queue queue tlist; // Ensure file pointer is at the beginning of the file ifileptr.seekg(0,ios::beg); // Go through file and load into list (from beginning) while((!ifileptr.eof()) && (tlist.size < maxsize)) { // Read in the number from file ifileptr >> temp; // Check whether list is full if(((tlist.size+1) == maxsize) && (temp != 0)) { // Add to list tlist.addToList(temp); // Find the place in file fileplace = ifileptr.tellg(); } else if((tlist.size < maxsize) && (temp != 0) ) { // Add to list tlist.addToList(temp); } } // Grab last prime ifileptr.seekg(-50,ios::end); while(!ifileptr.eof()) { ifileptr >> temp; } // Reset File Pointer ifileptr.seekg(0,ios::beg); // Check for file if(temp == 0) { // Add the first two prime numbers to the list // tlist.addToList(2); tlist.addToList(3); // Set Temp to last known value temp = 3; } // Initialise Add add = 1; // Go up until the number for( unsigned long int i = temp+1;i < num;i+=add) { // Initialisation // Initialise found found = false; // Reset File ifileptr.clear(); ifileptr.seekg(0,ios::beg); // Reset File ifileptr.clear(); // Get the max divisible number sqr = sqrt(i); // Intialise temp temp = 0; // Print out every 5000 if(((i+1) % 5000)==0) { cout << i << "\n"; } // Check for divisibility by 2 and then increment by two if necessary if((add!=2) && ((i%2)==1)) { add = 2; } // Memory // Reset List tlist.setToHead(); // Go through all know primes and check while((!tlist.eol) && (tlist.crecord->prime <= (sqr+1))) { // Check if its not a prime if((i % tlist.crecord->prime) == 0) { // Not a Prime found = true; break; }; // Move on one tlist.moveOnOne(); }; // FILE // Check for found if((found != true) && (tlist.size >= maxsize)) { // Set file pointer to the end of the list ifileptr.seekg(fileplace,ios::beg); // Check file // Check that it can still be prime // Check that the number is less than the square // Check that it is not the end of file while((found != true) & (temp<=(sqr+1)) & (!ifileptr.eof())) { // Read in the lines // Read from File ifileptr >> temp; if(!ifileptr.eof()) { if((i % temp) == 0) { // Found found = true; break; } } } }// End of Check if // Check For Prime found // Check for prime if (!found) { // Check for need of pagefile if((tlist.size+1) == maxsize) { // Add to list tlist.addToList(i); // Initialise temp temp = 0; // Get File size ifileptr.seekg(0,ios::end); // Put to near end of file if(ifileptr.tellg() <100 ) { // Go to beginning ifileptr.seekg(0,ios::beg); } else { // Go to near end ifileptr.seekg(100,ios::end); } // Go to near end of file ifileptr.seekg(-50,ios::end); // Go through file while(!(ifileptr.eof())) { ifileptr >> temp; } // Put put pointer to correct place ofileptr.seekp(0,ios::end); // Set temprecord to the head tlist.setToHead(); // Go through list while(tlist.crecord != NULL) { // Check for the right place to start if(temp < tlist.crecord->prime) { // Write to file ofileptr << tlist.crecord->prime << "\n"; } // Read in the next in the list tlist.moveOnOne(); } // Find the place in file fileplace = ifileptr.tellg(); // Set put pointer to the end ofileptr.seekp(0,ios::end); } else if(tlist.size >= maxsize) { // Dump to File // Convert from number to letter sprintf(numb,"%d",i); // Write the number to file ofileptr << numb << endl; } else { // Add the prime to the list tlist.addToList(i); } }; }; // End of File Dump // Check for dumped if(!(tlist.size >= maxsize)) { // File didnt fill list // Set File Pointer to beginning ofileptr.seekp(0,ios::beg); // Set record to head tlist.setToHead(); if(tlist.crecord != NULL) { // Finished load all primes into file while(!tlist.eol) { sprintf(numb,"%d",tlist.crecord->prime); // Write the number to file ofileptr << numb << "\n"; // Move list on one tlist.moveOnOne(); }; } } ifileptr.close(); ofileptr.close(); cout << "Thank You"; } stdafx.h // stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // #if !defined(AFX_STDAFX_H__0475B6EC_188A_4437_B811_1E42BBB8D475__INCLUDED_) #define AFX_STDAFX_H__0475B6EC_188A_4437_B811_1E42BBB8D475__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <stdio.h> #include <iostream> using std::ios; #include <fstream> #include <math.h> // Use the io class using std::ofstream; using std::ifstream; using std::cout; using std::cin; using std::endl; // The list class queue { // Type Declarations struct listrecord; // The Pointer typedef listrecord *listptr; // A listrecord struct listrecord { unsigned long int prime; listptr next; }; // Variable Declarations public: // Vars listptr head; // Head listptr tail; // Tail long int size; // Current Size of List listptr crecord; // Current Record bool eol; // Boolean value for End Of List // Function Declarations public : // Add To Linked list (at tail) void addToList(long int number) { // Prime found (Add to list) crecord = new(listrecord); crecord->prime = number; // Check there are primes if(tail != NULL) { tail->next = crecord; } else { // No primes, add head etc head = crecord; } // Set the Tail tail = crecord; // Set the termination value tail->next = NULL; // Increment the size size++; } // Delete from List Declaration void deleteFromList() { // Move on one head = head->next; // Delete the node from memory delete(crecord); // Set The Record to the next record on crecord = head; // Decrease the list size size-= 1; } // Move the crecord on one void moveOnOne() { // Move on one crecord = crecord->next; // Check for EOL if(crecord == NULL) { // Set eol to true eol = true; } else { // Set eol to false eol = false; } } // Set crecord to head void setToHead() { // Set Crecord to head crecord = head; // Check for EOL if(crecord == NULL) { // Set eol to true eol = true; } else { // Set eol to false eol = false; } } public : // Class Constructor queue() { // Initialise head = NULL; tail = NULL; size = 0; eol = false; } }; // TODO: reference additional headers your program requires here //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__0475B6EC_188A_4437_B811_1E42BBB8D475__INCLUDED_) As I said it sucks rather horribly and is a bit buggy but it can calculate the first million primes in 1 or 2 seconds. I'll probably get around to rewriting it in C or something at some point but there's not really much point as there are things that calculate primes far quicker. I think it's a bit buggy with the file handling (as well as other stuff) so if you do run it, it's probably best to tell it to store around the number of primes you want in memory (or say 1/5 or so of that number), otherwise it may stall. It didn't originally as I did use it to calculate all of them up to around a billion or so but it's crappy code and I didn't really know much about portability then and was writing it with dev c++ on windows. Oh well, memories
-
Yeah, it was just hacked together so there are a few issues with the leading zeroes etc. I'll tidy it up in the next few days and post a nicer version. The idea is simple enough, it just does long multiplication on base N numbers stored in an array, atm it uses 10 as the base for simplicity. Sadly, it'll get slower as time goes on as long multiplication between longer numbers requires more operations, but I guess that's the case with most arbitrary precision number classes (although I imagine most precision number classes have a lot more operations and are a lot more sophisticated )
-
http://pastie.caboo.se/68663 I haven't finished it yet but it does the basics (does around 5000! within a few seconds for me). I'll tidy it up and fix it up tonight so it can use int[] instead and a much larger base so as to reduce multiplications (I need to write the base conversion bits first, I have started but not finished yet) and there's a few other things that should help it. The limit of 200000 digits is arbitrary as I couldn't be arsed to expand the array etc every so often but it's easy enough to do or to use a formula to work out the number of digits.
-
remote desktop connection cmd questions
Aeternus replied to Ice_Phoenix87's topic in Computer Science
It's easier to remember when you know that mstsc stands for "MicroSoft Terminal Services Client", at least in my opinion. I could never remember the name of the executable until it twigged. -
I'd like to give a nice detailed reply but I have my last exam tomorrow, so I'll just give you a short answer and elaborate later I'm a little insulted at the calling of CS as an easy degree but I can understand it in some ways, not all degrees are equal, I wouldn't call it an easy degree by far but I would certainly say that there are degrees which are harder (such as physics or maths for instance). I'd say that this will mostly depend on the course and university at which you study, and on your own strengths and weaknesses (I don't think you can always say one degree is easier than another when the two require completely different types of thinking). Computer science as a subject can be very rich, complex and rewarding topic, sneaking quite deeply into maths sometimes (a large portion of our computer science research lecturers are mathematicians and a lot of our modules are maths based modules), but it all depends on the course and more importantly what you try and get out of your degree. Often, just following the course and taking no interest in peripheral topics might be unrewarding (again depends on course, but this is fairly general for all subjects I'd say). Perhaps if you suggest what you think Computer Science is likely to entail, what sort of student you are and so on, I/we can give you a better answer?
-
It looks like Pascal but the comments aren't right so I'm going to assume Delphi.
-
http://www.google.com/search?client=opera&rls=en&q=vista+vs+osx&sourceid=opera&ie=utf-8&oe=utf-8 I'm pretty sure this has been done to death in lots of different articles Although I'm sure some users may offer personal opinions.
-
Like... ? Examples man, Examples!
-
And the lack of deadlocks and shared-nothing benefit are all due to the fact that cross-thread communication is done via message passing rather than explicitly shared memory? (not that on the same system message passing might not be facilitated by shared memory but that the concept may be abstracted so when things are moved onto different computers the same concept can be applied with sockets etc)
-
So how does it handle the concurreny the way it does under the hood? You mentioned thread (on IRC), how do they work exactly ?
-
For Java I'd say the first one. See http://java.sun.com/docs/codeconv/html/CodeConventions.doc10.html#182 For C, I'd say do whatever the hell you like, just be consistent and if writing with others or on a project, stick to their standards (don't for the love of god go changing their code to suit your ideology unless it's already been decided by the group ) Yes, LISP and Python are cool (I really should code some more lisp, never really did all that much with it...)