Advertisement
Kaelygon

Untitled

May 8th, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. //license WTFPL
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <math.h>
  6. #include <iomanip>
  7. #include <bitset>
  8. #include <cstring>
  9.  
  10. #include<iostream>
  11. #include<fstream>
  12. #include <vector>
  13. #include <sstream>
  14. #include <algorithm>
  15. #include <iterator>
  16.  
  17. #define MAX__UINT128 (__uint128_t)(-1)
  18. #define MAX__UINT64 (__uint64_t)(-1)
  19.  
  20.  
  21. using namespace std;
  22.  
  23. __uint64_t bigP;
  24. __uint64_t bigPif; //biggest prime in file
  25.  
  26. vector<__uint128_t> a1v;
  27. string filep="./data/primes.bin";
  28. ofstream filepw(filep, ios::out | ios::binary | ios::app );
  29.  
  30. //BOF kmath
  31.  
  32. __uint128_t ui128pow( __uint128_t m, __uint128_t n) //128-bit pow
  33. {
  34. __uint128_t o=m;
  35.  
  36. while(n>1){
  37. n--;
  38. o*=m;
  39. }
  40. return o;
  41. }
  42.  
  43. //table of 10s
  44. __uint128_t tabp10[39]={ //10, 100 ... 10^39
  45. ui128pow(10,1), ui128pow(10,2), ui128pow(10,3), ui128pow(10,4), ui128pow(10,5), ui128pow(10,6), ui128pow(10,7), ui128pow(10,8), ui128pow(10,9), ui128pow(10,10), ui128pow(10,11), ui128pow(10,12), ui128pow(10,13), ui128pow(10,14), ui128pow(10,15), ui128pow(10,16), ui128pow(10,17), ui128pow(10,18), ui128pow(10,19), ui128pow(10,20), ui128pow(10,21), ui128pow(10,22), ui128pow(10,23), ui128pow(10,24), ui128pow(10,25), ui128pow(10,26), ui128pow(10,27), ui128pow(10,28), ui128pow(10,29), ui128pow(10,30), ui128pow(10,31), ui128pow(10,32), ui128pow(10,33), ui128pow(10,34), ui128pow(10,35), ui128pow(10,36), ui128pow(10,37), ui128pow(10,38), ui128pow(10,39)
  46. };
  47.  
  48. //count digits
  49. template <class T>
  50. T ui128log10(T n)
  51. {
  52. return
  53. (n < tabp10[0] ? 0 :
  54. (n < tabp10[1] ? 1 :
  55. (n < tabp10[2] ? 2 :
  56. (n < tabp10[3] ? 3 :
  57. (n < tabp10[4] ? 4 :
  58. (n < tabp10[5] ? 5 :
  59. (n < tabp10[6] ? 6 :
  60. (n < tabp10[7] ? 7 :
  61. (n < tabp10[8] ? 8 :
  62. (n < tabp10[9] ? 9 :
  63. (n < tabp10[10] ? 10 :
  64. (n < tabp10[11] ? 11 :
  65. (n < tabp10[12] ? 12 :
  66. (n < tabp10[13] ? 13 :
  67. (n < tabp10[14] ? 14 :
  68. (n < tabp10[15] ? 15 :
  69. (n < tabp10[16] ? 16 :
  70. (n < tabp10[17] ? 17 :
  71. (n < tabp10[18] ? 18 :
  72. (n < tabp10[19] ? 19 :
  73. (n < tabp10[20] ? 20 :
  74. (n < tabp10[21] ? 21 :
  75. (n < tabp10[22] ? 22 :
  76. (n < tabp10[23] ? 23 :
  77. (n < tabp10[24] ? 24 :
  78. (n < tabp10[25] ? 25 :
  79. (n < tabp10[26] ? 26 :
  80. (n < tabp10[27] ? 27 :
  81. (n < tabp10[28] ? 28 :
  82. (n < tabp10[29] ? 29 :
  83. (n < tabp10[30] ? 30 :
  84. (n < tabp10[31] ? 31 :
  85. (n < tabp10[32] ? 32 :
  86. (n < tabp10[33] ? 33 :
  87. (n < tabp10[34] ? 34 :
  88. (n < tabp10[35] ? 35 :
  89. (n < tabp10[36] ? 36 :
  90. (n < tabp10[37] ? 37 :
  91. tabp10[38]
  92. ))))))))))))))))))))))))))))))))))))))
  93. ;
  94. }
  95.  
  96. template <class T>
  97. T ui128pow10(T n)
  98. {
  99. return tabp10[n];
  100. }
  101.  
  102.  
  103. template <class T>
  104. T fsqrt128(T N) //return floor(sqrt(N))
  105. {
  106. T square = 1;
  107. T s=0;
  108. //find s that is s^3<N
  109. for(__int64_t i=ui128log10(N)-2; i>=0; i--){ //test increment 10^i , 10^(i-1) ... 10^2, 10^1
  110. T inc = ui128pow10(i);
  111. while(s*s<N){
  112. s+=inc;
  113. }
  114. s-=inc;
  115. }
  116. while(square<N){ //1 increments
  117. s+=1;
  118. square=s*s;
  119. }
  120. if(s*s!=N){
  121. s-=1;
  122. }
  123. return s;
  124.  
  125. }
  126.  
  127. //EOF kmath
  128.  
  129. //BOF debug
  130.  
  131. void printP(){
  132. for(auto &n : a1v)
  133. {
  134. bitset<64>sthalf=n>>64;
  135. bitset<64>ndhalf=n;
  136.  
  137. cout << ndhalf.to_ulong() << ",";
  138. }
  139. cout << "\n";
  140. }
  141.  
  142. //EOF debug
  143.  
  144. //BOF write func
  145.  
  146.  
  147. ifstream::pos_type filesize(string filename)
  148. {
  149. ifstream in(filep, std::ifstream::ate | std::ifstream::binary);
  150. return in.tellg();
  151. }
  152.  
  153.  
  154. void frst( string ffil="./data/primes.bin" ){ //file reset
  155. ofstream ffilrst(ffil, ios::out | ios::binary );
  156. ffilrst << "";
  157. ffilrst.close();
  158. }
  159.  
  160. int cArrayP(__uint64_t n){//test array divisors. 0==not prime, 1==prime, 2==in array, 3==bigger than bigP
  161.  
  162. for(auto &P : a1v) //go through array of primes
  163. {
  164. if( n%__uint128_t( (P) ) == 0 ){
  165. if(n==P){
  166. return 2;
  167. }
  168. return 0;
  169. }
  170.  
  171. if( P>fsqrt128(n) ){
  172. return 1;
  173. }
  174. }
  175.  
  176. return 3;
  177. }
  178.  
  179. void writeP(__uint64_t n){
  180. if( find(a1v.begin(), a1v.end(),n)==a1v.end() ){ //check if n is in array
  181. a1v.push_back(n);
  182. if(bigP<n){
  183. bigP=n;
  184. }
  185. }
  186. }
  187.  
  188. string ui128tos(__uint128_t n){//128 uint to string
  189. string out = "";
  190.  
  191. while(n!=0){
  192. out+=to_string( (__uint8_t)(n%10) );
  193. n/=10;
  194. }
  195.  
  196. reverse(out.begin(), out.end());
  197.  
  198. return out;
  199. }
  200.  
  201. void wtofile(){
  202. for(auto &P : a1v) //go through array of primes
  203. {
  204. if(P<=bigPif){continue;} //skip if smaller than biggest prime in array
  205.  
  206. string pstr = ui128tos(P);
  207. filepw << pstr; //write to file
  208. filepw << ',';
  209. }
  210. }
  211.  
  212. //EOF write func
  213.  
  214.  
  215. int main( int argc, char *argv[] ){
  216. string str;
  217. ifstream filepr(filep, ios::in | ios::binary );
  218. filepr >> str;
  219. filepr.close();
  220.  
  221. stringstream ss(str);
  222.  
  223. //assuming that the file includes all the primes smaller than last prime, sorted ascending
  224. if( filesize(filep) > 4 ){ //if string stream is bigger than 4 bytes (2,3,)
  225. for (int i; ss >> i;) {
  226. a1v.push_back(i);
  227. if (ss.peek() == ',')
  228. ss.ignore();
  229. }
  230.  
  231. bigP=a1v[a1v.size()-1];
  232. bigPif=bigP;
  233. }else{ //set two first primes if file is > 4B
  234. a1v.push_back(2);
  235. a1v.push_back(3);
  236.  
  237. bigP=3;
  238. bigPif=0;
  239. }
  240.  
  241. //clear
  242. ss.str("");
  243.  
  244. cout << "size " << a1v.size() << " bigP " << bigP << "\n";
  245.  
  246. int s=10000; //check and write primes up to s, ceil sqrt(num)
  247.  
  248. int si=bigP;
  249. if( si%2==0 ){ //make sure it's odd
  250. si-=1;
  251. }
  252.  
  253. for(si;si<s;si+=2){ // bigP to s
  254.  
  255. bool siisP =cArrayP(si);
  256.  
  257. if(siisP==1){
  258. writeP(si);
  259. }
  260.  
  261. }
  262.  
  263. // __uint64_t found=0;
  264.  
  265. wtofile();
  266.  
  267. // cout << "found: " << found << "\n";
  268. cout << "wrote " << a1v.size() << " primes\n";
  269. filepw.close();
  270. }
  271.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement