![]() |
|
|
#45 | ||
|
"Ben"
Feb 2007
DB916 Posts |
Quote:
Quote:
Code:
s=Mod(0,10^4);forprime(p=2,1e9,s+=p^2;if(!s,return(p))) Code:
p=2;s=0;ten=10;for(i=2,10000000,s=s+p;if(Mod(s,ten)==0, ten=ten*10;print(i,":",p, ":",s)); p=nextprime(p+1)) |
||
|
|
|
|
|
#46 | |
|
Mar 2010
Brick, NJ
67 Posts |
Quote:
The best example may be I frankly could not understand the first mathematical notation I read to explain the Lucas-Lehmer test, but looking at the code it understood the notation that was used immediately. As far as the YAFU code, I was under the impression that you a simple PARI like expression was entered into YAFU. I am also interested in how all primes up to 10 trillion have been calculated, or have I read it wrong and that is the sum of the primes is over 10 Trillion. Last fiddled with by alexhiggins732 on 2010-04-10 at 02:02 |
|
|
|
|
|
|
#47 | ||
|
"Ben"
Feb 2007
3×1,171 Posts |
Quote:
The way it's written, I doubt the YAFU code would make anything clearer to you .Quote:
.The sum is a number with 26 digits, and the square sum is a number with 39 digits. The primes are calculated using the sieve of eratosthenes. That code is public domain... just download YAFU-1.18 and browse around through the soe.c file. Although it's not well documented .I compute the primes in batches of 1 billion integers. At a height of 10 trillion, that is 32 million primes or so per batch. It's then pretty straightfoward to sum them and check each for divisiblity by a power of 10 (sweeping many optimizations under the rug, for clarity). On my (admittedly fast) computer, each batch takes about 3 seconds for prime computation, and .2 seconds for the sum/check. This is the only code that's not public yet. Although yafu-1.19 will probably retain it when it comes out. Last fiddled with by bsquared on 2010-04-10 at 02:24 Reason: respond to edit |
||
|
|
|
|
|
#48 | |
|
Mar 2010
Brick, NJ
1038 Posts |
Quote:
Here's .Net Code If you wish to post as well. VB Code:
dim ten as bigint=10:dim sum as bigint=2:for each prime in primesbelow(10000000):if ten.mod(sum)=0 then : ten*=10 : console.writeline("{0}:{1}:{2}",i,p,s): end if: sum+=prime: next
Code:
bigint ten = 10;bigint sum = 2; foreach (var prime in primesbelow(10000000)) { if (ten.mod(sum) == 0) {ten *= 10; console.writeline("{0}:{1}:{2}", i, p, s); } sum += prime; }}
Last fiddled with by alexhiggins732 on 2010-04-10 at 03:17 |
|
|
|
|
|
|
#49 | |
|
Mar 2010
Brick, NJ
67 Posts |
Quote:
However, for all primes below 2^32 the file size is 178,956,968 bytes (170 MB) and it doubles with each power of two which led me to ask how how you could sieve up to 2^44 which would require a huge amount of memory even when saved in the most compact possible form (1 bit for each 6K+=1) Which brings up another point. If the distribution of primes are indeed random, how then is it possible to compress the aforementioned file and receive a compression ratio of 60% (eg 170mb compresses to 102MB)? Last fiddled with by alexhiggins732 on 2010-04-10 at 02:50 Reason: wrong compression ratio |
|
|
|
|
|
|
#50 | |
|
Aug 2006
597910 Posts |
If you use my code, be sure to change "4" to "n" (and perhaps prefix "a(n)=").
Quote:
Last fiddled with by CRGreathouse on 2010-04-10 at 03:42 |
|
|
|
|
|
|
#51 | |
|
Aug 2006
175B16 Posts |
Quote:
|
|
|
|
|
|
|
#52 | |
|
"Ben"
Feb 2007
3·1,171 Posts |
Quote:
Code:
yafu "primes(10000000000000,10001000000000)" -v -v Code:
elapsed time for seed primes = 0.0144 sieving range 9999999999840 to 10001006632800 using 227655 primes, max prime = 3162436 using 8 residue classes lines have 4194304 bytes and 33554432 flags lines broken into 128 blocks of size 32768 blocks contain 262144 flags and cover 7864320 primes bucket sieving 194320 primes > 393216 allocating space for 62341 hits per bucket using 45026396 bytes for sieving storage elapsed time = 1.0610 ans = 33405006 Once I know the locations of the primes in the bit arrays, I compute them, but only the 33 odd million of them, of course, not a billion. Stored as 64 bit integers, this takes about 250 MB of RAM and another 2 seconds or so. |
|
|
|
|
|
|
#53 | |
|
"Ben"
Feb 2007
3·1,171 Posts |
Quote:
To save on the forum database, its attached rather than posted, but keep in mind that this won't compile as is. - ben. |
|
|
|
|
|
|
#54 |
|
"Ben"
Feb 2007
3×1,171 Posts |
The routine to do prime cubes is very similar, except for the assembly to do the cube/sum:
Code:
ASM_G ( "movq %1, %%rcx \n\t" /* store prime */ "mulq %%rcx \n\t" /* square it */ "movq %%rax, %%r8 \n\t" /* save p^2 lo (a) */ "movq %%rdx, %%r9 \n\t" /* save p^2 hi (d) */ "mulq %%rcx \n\t" /* p * a */ "movq %%rax, %%r10 \n\t" /* save p*a lo (apa) */ "movq %%rdx, %%r11 \n\t" /* save p*a hi (apd) */ "movq %%r9, %%rax \n\t" /* p * d */ "mulq %%rcx \n\t" /* lo part in rax (dpa), hi in rdx (dpd) */ "addq %%r10, (%%rbx) \n\t" /* sum0 = sum0 + apa */ "adcq %%rax, 8(%%rbx) \n\t" /* sum1 = sum1 + dpa + carry */ "adcq %%rdx, 16(%%rbx) \n\t" /* sum2 = sum2 + dpd + carry */ "addq %%r11, 8(%%rbx) \n\t" /* sum1 = sum1 + apd */ "adcq $0, 16(%%rbx) \n\t" /* sum2 = sum2 + carry */ : : "b"(sum->val), "a"(PRIMES[j]) : "rcx", "rdx", "r8", "r9", "r10", "r11", "memory", "cc"); Last fiddled with by bsquared on 2010-04-10 at 05:43 Reason: have I mentioned I loath code formatting in vBulletin? |
|
|
|
|
|
#55 | |
|
Mar 2010
Brick, NJ
67 Posts |
Quote:
So being the crank I am, I did what any self-respecting crank would do, I rewrote it in VB.NET, but have a few problems perhaps you can shine some light on. I tried, for the most part, to use the exact vb equivalent of the c code 1) The multi-threading is not functional, yet. I understand the logic that you are implementing so I just may rewrite that and deviate from adhering strictly to the original code. I couldn't really figure out what thread_soedata_t->thread_id was being used for. I am not sure what the vb equivalent of CreateThread would be. In any case, I can rewrite the threading. Each thread is assigned a command, waits until it is signal to run then notifies the master thread when done, rinse lather repeat. 2) When you declare a local variable of a structure member a value copy is created on the heap and the original structure is not updated. If this where declared a class, it would work like the c implementation but I didn't what to do that. So I had to rewrite as follows: Code:
Public Sub primes_from_lineflags(ByRef t As thread_soedata_t)
' this is no good. it create value copy on the heap so the original
' thread_soedata_t members do not get updated.
'Dim ddata As soe_dynamicdata_t = t.ddata
'Dim sdata As soe_staticdata_t = t.sdata
' work around is to pass a pointer to the structure and access the
' the members using the pointer like on the next line.
Dim line() As Byte = t.ddata.line
However, not being able to see that values are in those arrays in the debugger and being a crank that is trying to learn what is going on from the code I don't know if the values in the vb arrays are correct. Maybe my misinterpretation of how pointers work is the problem. For example, interpreted flagblock += BLOCKSIZE; as increment the index of the flagblock + blocksize. Since this is illegal in vb, to accommodate I created a new variable fob and instead of flagblock + BLOCKSIZE, its fob+BLOCKSIZE and then when accessing flagblock its flagblock[x+fob]. Is this wrong? Perhaps the simplest solution is to attach the code I have and hopefully someone with MSVS can debug it and give me a hint whats wrong. Its just a little frustrating after rewriting 2000 lines of code.... Oh well, maybe I'll write it in C# so I can use the pointer arithmetic and just import the DLL into my vb projects
|
|
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Regarding Squares | a1call | Miscellaneous Math | 42 | 2017-02-03 01:29 |
| Basic Number Theory 12: sums of two squares | Nick | Number Theory Discussion Group | 0 | 2016-12-11 11:30 |
| Integers = sums of 2s and 3s. | 3.14159 | Miscellaneous Math | 12 | 2010-07-21 11:47 |
| Sums of three squares | CRGreathouse | Math | 6 | 2009-11-06 19:20 |
| squares or not squares | m_f_h | Puzzles | 45 | 2007-06-15 17:46 |