Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91263 > unrolled thread

Sieve of Eratosthenes

Started byBonita Montero <Bonita.Montero@gmail.com>
First post2023-08-16 15:37 +0200
Last post2023-08-30 19:02 +0200
Articles 6 on this page of 146 — 13 participants

Back to article view | Back to comp.lang.c++


Contents

  Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-16 15:37 +0200
    Re: Sieve of Eratosthenes wij <wyniijj5@gmail.com> - 2023-08-16 10:57 -0700
      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-16 20:21 +0200
        Re: Sieve of Eratosthenes wij <wyniijj5@gmail.com> - 2023-08-16 12:17 -0700
      Re: Sieve of Eratosthenes Paavo Helde <eesnimi@osa.pri.ee> - 2023-08-16 21:44 +0300
        Re: Sieve of Eratosthenes wij <wyniijj5@gmail.com> - 2023-08-16 12:09 -0700
    Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-16 23:17 +0200
      Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-16 23:21 +0200
      Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-16 23:39 +0200
        Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-16 23:59 +0200
          Re: Sieve of Eratosthenes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-08-16 16:27 -0700
      Re: Sieve of Eratosthenes red floyd <no.spam.here@its.invalid> - 2023-08-16 18:06 -0700
        Re: Sieve of Eratosthenes Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-16 19:26 -0700
          Re: Sieve of Eratosthenes Öö Tiib <ootiib@hot.ee> - 2023-08-27 12:26 -0700
            Re: Sieve of Eratosthenes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-28 00:49 +0100
              Re: Sieve of Eratosthenes Öö Tiib <ootiib@hot.ee> - 2023-08-27 23:23 -0700
                Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-28 15:53 +0200
                  Re: Sieve of Eratosthenes Öö Tiib <ootiib@hot.ee> - 2023-08-28 09:03 -0700
                    Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-28 18:47 +0200
                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-30 13:01 +0200
                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-30 15:38 +0000
                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-30 18:31 +0200
                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-31 08:22 +0000
                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 11:00 +0200
                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-31 15:23 +0000
                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 17:28 +0200
                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 11:52 +0200
                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-31 15:22 +0000
                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 17:32 +0200
                                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-31 15:44 +0000
                                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 17:50 +0200
                                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 10:26 +0000
                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 12:59 +0200
                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 12:30 +0000
                                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 15:33 +0200
                                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 13:49 +0000
                                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 15:59 +0200
                                                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 14:03 +0000
                                                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 16:15 +0200
                                                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 14:39 +0000
                                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 16:59 +0200
                                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 15:01 +0000
                                                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 17:05 +0200
                                                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-09-01 15:08 +0000
                                                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 17:15 +0200
                                Re: Sieve of Eratosthenes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-31 21:33 +0100
                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-09-01 05:35 +0200
                              Re: Sieve of Eratosthenes scott@slp53.sl.home (Scott Lurndal) - 2023-08-31 14:43 +0000
                                Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 16:57 +0200
                                  Re: Sieve of Eratosthenes scott@slp53.sl.home (Scott Lurndal) - 2023-08-31 16:10 +0000
                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-31 15:25 +0000
                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-31 17:33 +0200
                                    Re: Sieve of Eratosthenes scott@slp53.sl.home (Scott Lurndal) - 2023-08-31 16:12 +0000
                  Re: Sieve of Eratosthenes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-28 23:08 +0100
              Re: Sieve of Eratosthenes Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-28 15:15 -0700
                Re: Sieve of Eratosthenes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-28 23:41 +0100
                  Re: Sieve of Eratosthenes Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-30 17:45 -0700
                    Re: Sieve of Eratosthenes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-08-31 03:18 +0100
            Re: Sieve of Eratosthenes Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-28 15:08 -0700
        Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-17 08:42 +0200
          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-17 11:38 +0200
            Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-17 12:50 +0200
              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-17 13:10 +0200
              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-17 17:47 +0200
              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-21 11:00 +0200
                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-21 17:19 +0000
                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-21 19:50 +0200
                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-21 18:12 +0000
                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-21 20:22 +0200
                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-21 19:01 +0000
                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 05:27 +0200
                            Re: Sieve of Eratosthenes Öö Tiib <ootiib@hot.ee> - 2023-08-21 23:25 -0700
                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 11:23 +0200
                                Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 11:59 +0200
                                Re: Sieve of Eratosthenes Öö Tiib <ootiib@hot.ee> - 2023-08-22 05:15 -0700
                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 14:23 +0200
                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 06:56 +0000
                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 11:27 +0200
                                Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 11:34 +0200
                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 14:13 +0000
                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 16:17 +0200
                                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 14:27 +0000
                                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 16:48 +0200
                                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 14:57 +0000
                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 17:00 +0200
                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 17:56 +0000
                                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 20:13 +0200
                                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 18:18 +0000
                                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-22 20:50 +0200
                                                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 06:51 +0000
                                                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 09:59 +0200
                                                        Re: Sieve of Eratosthenes Richard Damon <Richard@Damon-Family.org> - 2023-08-23 07:52 -0400
                                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 14:40 +0200
                                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 14:24 +0000
                                                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 16:30 +0200
                                                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 14:36 +0000
                                                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 16:38 +0200
                                                                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 14:43 +0000
                                                                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 16:45 +0200
                                                                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 17:49 +0000
                                                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 20:05 +0200
                                                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-24 17:18 +0000
                                                                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-24 19:20 +0200
                                                                          Re: Sieve of Eratosthenes Daniel <danielaparker@gmail.com> - 2023-08-23 11:13 -0700
                                                                            Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 20:17 +0200
                                                                              Re: Sieve of Eratosthenes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-08-23 11:20 -0700
                                                                                Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 20:24 +0200
                                                                                  Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-24 17:25 +0000
                                                                                    Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-24 19:29 +0200
                                                                                      Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-24 17:40 +0000
                                                                                        Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-24 19:49 +0200
                                                                                          Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-25 16:07 +0000
                                                                                            Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 18:10 +0200
                                                                                              Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-25 16:15 +0000
                                                                                                Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-25 18:18 +0200
                                                                                                  Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-26 08:03 +0000
                                                                                                    Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-26 10:16 +0200
                                                                                                      Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-26 08:27 +0000
                                                                                Re: Sieve of Eratosthenes Daniel <danielaparker@gmail.com> - 2023-08-23 11:37 -0700
                                                                                  Re: Sieve of Eratosthenes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-08-26 15:33 -0700
                                                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 14:20 +0000
                                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 16:32 +0200
                                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 14:41 +0000
                                                              Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 16:47 +0200
                                                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 17:51 +0000
                                                                  Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-23 20:09 +0200
                                                                    Re: Sieve of Eratosthenes scott@slp53.sl.home (Scott Lurndal) - 2023-08-23 18:51 +0000
                                                                      Re: Sieve of Eratosthenes "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-08-24 11:25 -0700
                                                                    Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-24 17:22 +0000
                                                                      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-24 19:27 +0200
                                                                        Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-24 17:37 +0000
                                                                          Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-24 19:52 +0200
                                                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-25 16:07 +0000
                                                          Re: Sieve of Eratosthenes Daniel <danielaparker@gmail.com> - 2023-08-23 08:00 -0700
                                          Re: Sieve of Eratosthenes scott@slp53.sl.home (Scott Lurndal) - 2023-08-22 15:49 +0000
                                            Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-22 17:58 +0000
                                              Re: Sieve of Eratosthenes scott@slp53.sl.home (Scott Lurndal) - 2023-08-22 18:24 +0000
                                                Re: Sieve of Eratosthenes Muttley@dastardlyhq.com - 2023-08-23 06:50 +0000
      Re: Sieve of Eratosthenes wij <wyniijj5@gmail.com> - 2023-08-17 13:43 -0700
        Re: Sieve of Eratosthenes "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-08-18 02:00 +0200
          Re: Sieve of Eratosthenes wij <wyniijj5@gmail.com> - 2023-08-18 05:01 -0700
            Re: Sieve of Eratosthenes wij <wyniijj5@gmail.com> - 2023-08-18 17:02 -0700
    Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-28 06:15 +0200
    Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-28 09:43 +0200
    Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-29 20:34 +0200
      Re: Sieve of Eratosthenes Bonita Montero <Bonita.Montero@gmail.com> - 2023-08-30 19:02 +0200

Page 8 of 8 — ← Prev page 1 2 3 4 5 6 7 [8]


#91293

Fromwij <wyniijj5@gmail.com>
Date2023-08-18 05:01 -0700
Message-ID<a39c1540-398f-457b-a468-8c7f798cbaebn@googlegroups.com>
In reply to#91291
On Friday, August 18, 2023 at 8:00:49 AM UTC+8, Alf P. Steinbach wrote:
> On 2023-08-17 10:43 PM, wij wrote: 

> > 3. I don't know what "fmt::print( f, "{:d}\n", i );" really mean, but in general 
> > I think cstring format in its current usage (e.g. "foo %s= %d\n") does not 
> > provide real benefit for the executing time spent interpreting it.
> With the {fmt} library's `fmt::print`, which will become C++23's 
> `std::print`, the format string is interpreted at compile time. 
> 
> That's almost like a lint implemented via C++ template system, plus that 
> it's just about the fastest string formatting around, plus handles 
> Unicode (guaranteed by the standard) if the output device can do it. 
> 
> The design of the compile time format string handling does however not 
> adhere to the principle of least surprise, and that caused the 
> standard's wording to be a bit sabotage-like for some time. That was 
> eventually fixed. But means that somewhere I have this C++20 code: 
> 
> #if __cpp_lib_format >= (2022*100 + 7) 
> #define myprefix_FMTLIB_API_PROVIDES_FORMAT_STRING_TYPE 
> constexpr bool api_provides_format_string_type = true; 
> 
> 
> - Alf

Not only time, but many kind of resources are used by std::print.

My idea of the 'format' issue was adding a format member to class String should
be enough, e.g. "String& String::fmt(..)", where the .. means unclear.

So, the usecase (example) printf("IC0123 qty:%5d, price:%.2f\n",qty,prc) can be 
translated like the following:

 result << String("IC0123 qty:") << wrd(qty).fmt("5") << ", price:"
        << wrd(prc).fmt(".2") << "\n";

 ('resutl' can be String or any device)

The main point is that such provision does not deserve any complexity adding to
the language, so burden to the user as well. Of course, who knows what the stdc++'s
head is thinking about.

[toc] | [prev] | [next] | [standalone]


#91303

Fromwij <wyniijj5@gmail.com>
Date2023-08-18 17:02 -0700
Message-ID<733c2c75-0290-4f8b-83be-51caa212ae9dn@googlegroups.com>
In reply to#91293
On Friday, August 18, 2023 at 8:01:58 PM UTC+8, wij wrote:
> On Friday, August 18, 2023 at 8:00:49 AM UTC+8, Alf P. Steinbach wrote: 
> > On 2023-08-17 10:43 PM, wij wrote: 
> 
> > > 3. I don't know what "fmt::print( f, "{:d}\n", i );" really mean, but in general 
> > > I think cstring format in its current usage (e.g. "foo %s= %d\n") does not 
> > > provide real benefit for the executing time spent interpreting it. 
> > With the {fmt} library's `fmt::print`, which will become C++23's 
> > `std::print`, the format string is interpreted at compile time. 
> > 
> > That's almost like a lint implemented via C++ template system, plus that 
> > it's just about the fastest string formatting around, plus handles 
> > Unicode (guaranteed by the standard) if the output device can do it. 
> > 
> > The design of the compile time format string handling does however not 
> > adhere to the principle of least surprise, and that caused the 
> > standard's wording to be a bit sabotage-like for some time. That was 
> > eventually fixed. But means that somewhere I have this C++20 code: 
> > 
> > #if __cpp_lib_format >= (2022*100 + 7) 
> > #define myprefix_FMTLIB_API_PROVIDES_FORMAT_STRING_TYPE 
> > constexpr bool api_provides_format_string_type = true; 
> > 
> > 
> > - Alf
> Not only time, but many kind of resources are used by std::print. 
> 
> My idea of the 'format' issue was adding a format member to class String should 
> be enough, e.g. "String& String::fmt(..)", where the .. means unclear. 
> 
> So, the usecase (example) printf("IC0123 qty:%5d, price:%.2f\n",qty,prc) can be 
> translated like the following: 
> 
> result << String("IC0123 qty:") << wrd(qty).fmt("5") << ", price:" 
> << wrd(prc).fmt(".2") << "\n"; 
> 
> ('resutl' can be String or any device) 
> 
> The main point is that such provision does not deserve any complexity adding to 
> the language, so burden to the user as well. Of course, who knows what the stdc++'s 
> head is thinking about.

Just came to me, libwy already had the basic capability of 'format' :

  wrd(i).indent('0',5)  // left align, width=5, fill-character='0';
  wrd(i).indent(5,' ')  // right align, width=5, fill-character=' ';
  wrd(val,10,2)  // float number=val, radix=10, fraction digits=2
  wrd(val, 0,2)  // float number=val, scientific notation, fraction digits=2

I never remember all these rules of format specifiers:
https://cplusplus.com/reference/cstdio/printf/

As to the example shown "constexpr int n = 10'000'000;", there are many kind of
preferences, e.g. "10,000,000" or "1000 0000",... or 'unicode'. IMO, these are
better be done by programmers, not in 'standard'.

[toc] | [prev] | [next] | [standalone]


#91453

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-08-28 06:15 +0200
Message-ID<uch71k$1i84o$1@dont-email.me>
In reply to#91263
I optimized the code somewhat further:

#include <iostream>
#include <vector>
#include <charconv>
#include <string_view>
#include <algorithm>
#include <fstream>
#include <cctype>
#include <cstring>
#include <bit>
#include "ndi.h"

#if defined(_MSC_VER)
	#pragma warning(disable: 26495) // uninitialized member
#endif

using namespace std;

int main( int argc, char **argv )
{
	constexpr size_t BUF_SIZE = 0x100000;
	try
	{
		size_t end;
		if( argc < 2 )
			return EXIT_FAILURE;
		char const *sizeStr = argv[1];
		bool hex = sizeStr[0] == '0' && (sizeStr[1] == 'x' || sizeStr[1] == 'X');
		sizeStr += 2 * hex;
		if( from_chars_result fcr = from_chars( sizeStr, sizeStr + strlen( 
sizeStr ), end, !hex ? 10 : 16 ); (bool)fcr.ec || *fcr.ptr )
			return EXIT_FAILURE;
		if( size_t e = end; ++end + 7 < e )
			throw bad_alloc();
		using bit_vec = vector<uint8_t>;
		using bit_it = bit_vec::iterator;
		bit_vec sieve( (end + 7) / 8, -1 );
		ofstream ofs;
		using ndi_vec = vector<ndi<char>>;
		using ndi_vec_it = ndi_vec::iterator;
		constexpr size_t AHEAD = 32;
		ndi_vec printBuf;
		ndi_vec_it bufBegin, bufEnd, bufFlush;
		if( argc < 3 || *argv[2] )
		{
			ofs.exceptions( ofstream::failbit | ofstream::badbit );
			ofs.open( argc >= 3 ? argv[2] : "primes.txt", ofstream::trunc );
			printBuf.resize( BUF_SIZE + AHEAD - 1 );
			bufBegin = printBuf.begin();
			bufEnd = bufBegin;
			bufFlush = bufBegin + BUF_SIZE;
		}
		auto print = [&]() { ofs << string_view( &*to_address( bufBegin ), 
&*to_address( bufEnd ) ); };
		for( size_t p = 2; p != end; )
		{
			bit_it pSieve = sieve.begin();
			for( size_t m = p; (m += p) < end; )
				pSieve[m / 8] &= ~(1u << m % 8);
			pSieve += ++p / 8;
			for( uint8_t m = 1u << p % 8, mNew; ; ++p )
			{
				if( p == end ) [[unlikely]]
					goto brk;
				if( *pSieve & m ) [[unlikely]]
					break;
				mNew = rotl( m, 1 );
				pSieve += mNew < m;
				m = mNew;
			}
			if( !ofs.is_open() )
				continue;
			auto [ptr, ec] = to_chars( &*bufEnd, &*to_address( bufEnd + (AHEAD - 
1) ), p );
			if( (bool)ec ) [[unlikely]]
				throw system_error( (int)ec, generic_category(), "converson failed" );
			bufEnd = ptr - &*bufBegin + bufBegin; // NOP
			*bufEnd++ = '\n';
			if( bufEnd >= bufFlush )
				print(),
				bufEnd = bufBegin;
		}
	brk:
		print();
		return EXIT_SUCCESS;
	}
	catch( bad_alloc const & )
	{
		cout << "out of memory" << endl;
	}
	catch( ios_base::failure const & )
	{
		cout << "I/O error" << endl;
	}
	catch( system_error const &se )
	{
		cout << se.what() << endl;
	}
}

[toc] | [prev] | [next] | [standalone]


#91456

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-08-28 09:43 +0200
Message-ID<uchj7h$1jshg$1@dont-email.me>
In reply to#91263
I further improved my code. Now scanning for the next prime is
signifiantly more efficient because I use a rotating bit mask.

#include <iostream>
#include <vector>
#include <charconv>
#include <string_view>
#include <algorithm>
#include <fstream>
#include <cctype>
#include <cstring>
#include <bit>

#if defined(_MSC_VER)
	#pragma warning(disable: 26495) // uninitialized member
#endif

using namespace std;

int main( int argc, char **argv )
{
	constexpr size_t BUF_SIZE = 0x100000;
	try
	{
		size_t end;
		if( argc < 2 )
			return EXIT_FAILURE;
		char const *sizeStr = argv[1];
		bool hex = sizeStr[0] == '0' && (sizeStr[1] == 'x' || sizeStr[1] == 'X');
		sizeStr += 2 * hex;
		if( from_chars_result fcr = from_chars( sizeStr, sizeStr + strlen( 
sizeStr ), end, !hex ? 10 : 16 ); (bool)fcr.ec || *fcr.ptr )
			return EXIT_FAILURE;
		if( size_t e = end; ++end + 7 < e )
			throw bad_alloc();
		using bit_vec = vector<uint8_t>;
		using bit_it = bit_vec::iterator;
		bit_vec sieve( (end + 7) / 8, -1 );
		ofstream ofs;
		union ndi_t { char c; ndi_t() {} };
		using ndi_vec = vector<ndi_t>;
		using ndi_vec_it = ndi_vec::iterator;
		constexpr size_t AHEAD = 32;
		ndi_vec printBuf;
		ndi_vec_it bufBegin, bufEnd, bufFlush;
		if( argc < 3 || *argv[2] )
		{
			ofs.exceptions( ofstream::failbit | ofstream::badbit );
			ofs.open( argc >= 3 ? argv[2] : "primes.txt", ofstream::trunc );
			printBuf.resize( BUF_SIZE + AHEAD - 1 );
			bufBegin = printBuf.begin();
			bufEnd = bufBegin;
			bufFlush = bufBegin + BUF_SIZE;
		}
		auto print = [&]() { ofs << string_view( &to_address( bufBegin )->c, 
&to_address( bufEnd )->c ); };
		for( size_t p = 2; p != end; )
		{
			bit_it pSieve = sieve.begin();
			for( size_t m = p; (m += p) < end; )
				pSieve[m / 8] &= ~(1u << m % 8);
			pSieve += ++p / 8;
			for( uint8_t m = 1u << p % 8, mNew; ; ++p )
				if( p == end ) [[unlikely]]
					goto brk;
				else if( *pSieve & m ) [[unlikely]]
					break;
				else
					mNew = rotl( m, 1 ),
					pSieve += mNew < m,
					m = mNew;
			if( !ofs.is_open() )
				continue;
			auto [ptr, ec] = to_chars( &bufEnd->c, &to_address( bufEnd + (AHEAD - 
1) )->c, p );
			if( (bool)ec ) [[unlikely]]
				throw system_error( (int)ec, generic_category(), "converson failed" );
			bufEnd = ptr - &bufBegin->c + bufBegin; // NOP
			bufEnd++->c = '\n';
			if( bufEnd >= bufFlush )
				print(),
				bufEnd = bufBegin;
		}
	brk:
		print();
		return EXIT_SUCCESS;
	}
	catch( bad_alloc const & )
	{
		cout << "out of memory" << endl;
	}
	catch( ios_base::failure const & )
	{
		cout << "I/O error" << endl;
	}
	catch( system_error const &se )
	{
		cout << se.what() << endl;
	}
}

[toc] | [prev] | [next] | [standalone]


#91467

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-08-29 20:34 +0200
Message-ID<ucldoh$2c6qt$1@dont-email.me>
In reply to#91263
I think that's my final solution. Currently I store the bits in
size_ts and I look up the next set bit with countr_zero which maps
to TZCNT on x64 and according to some small code on godbolt ARM
also has a proper instruction for that.
Resettig the bits for the composites is done by a rotating bit-mask.
If the prime is less than size_t * 8 there's a special case optimi-
zation.

#include <iostream>
#include <vector>
#include <charconv>
#include <string_view>
#include <algorithm>
#include <fstream>
#include <cctype>
#include <cstring>
#include <bit>

#if defined(_MSC_VER)
     #pragma warning(disable: 26495) // uninitialized member
#endif
#if defined(__llvm__)
     #pragma clang diagnostic ignored "-Wdangling-else"
#endif

using namespace std;

int main( int argc, char **argv )
{
     constexpr size_t BUF_SIZE = 0x100000;
     try
     {
         size_t end;
         if( argc < 2 )
             return EXIT_FAILURE;
         char const *sizeStr = argv[1];
         bool hex = sizeStr[0] == '0' && (sizeStr[1] == 'x' || 
sizeStr[1] == 'X');
         sizeStr += 2 * hex;
         if( from_chars_result fcr = from_chars( sizeStr, sizeStr + 
strlen( sizeStr ), end, !hex ? 10 : 16 ); (bool)fcr.ec || *fcr.ptr )
             return EXIT_FAILURE;
         if( !++end )
             throw bad_alloc();
         using word_t = size_t;
         constexpr size_t BITS = sizeof(word_t) * 8;
         size_t bEnd = (end + BITS - 1) & -(ptrdiff_t)BITS;
         if( bEnd < end )
             throw bad_alloc();
         union ndi_word_t { word_t w; ndi_word_t( word_t w ) : w( w ) {} 
    };
         using bit_vec = vector<ndi_word_t>;
         using bit_it = bit_vec::iterator;
         bit_vec sieve( (end + BITS - 1) / BITS, 
(word_t)0xAAAAAAAAAAAAAAAAu );
         ofstream ofs;
         union ndi_t { char c; ndi_t() {} };
         using ndi_vec = vector<ndi_t>;
         constexpr size_t AHEAD = 32;
         ndi_vec printBuf;
         ndi_vec::iterator bufBegin, bufEnd, bufFlush;
         if( argc < 3 || *argv[2] )
         {
             ofs.exceptions( ofstream::failbit | ofstream::badbit );
             ofs.open( argc >= 3 ? argv[2] : "primes.txt", 
ofstream::trunc );
             printBuf.resize( BUF_SIZE + AHEAD - 1 );
             bufBegin = printBuf.begin();
             bufEnd = bufBegin;
             bufFlush = bufBegin + BUF_SIZE;
         }
         auto print = [&]() { ofs << string_view( &to_address( bufBegin 
)->c, &to_address( bufEnd )->c ); };
         bit_it const pSieveEnd = sieve.end();
         size_t sqr = (ptrdiff_t)ceil( sqrt( (ptrdiff_t)end ) );
         for( size_t p = 2; p < end; )
         {
             if( ofs.is_open() )
             {
                 auto [ptr, ec] = to_chars( &bufEnd->c, &to_address( 
bufEnd + (AHEAD - 1) )->c, p );
                 if( (bool)ec ) [[unlikely]]
                     throw system_error( (int)ec, generic_category(), 
"converson failed" );
                 bufEnd = ptr - &bufBegin->c + bufBegin; // NOP
                 bufEnd++->c = '\n';
                 if( bufEnd >= bufFlush )
                     ofs << string_view( &to_address( bufBegin )->c, 
&to_address( bufEnd )->c ),
                     bufEnd = bufBegin;
             }
             if( ++p == end ) [[unlikely]]
                 break;
             for( bit_it pSieve = sieve.begin() + p / BITS; ; )
             {
                 size_t g = countr_zero( pSieve->w & (word_t)-1 << p % 
BITS );
                 p = (p & -(ptrdiff_t)BITS) + g;
                 if( g != BITS )
                     break;
                 if( ++pSieve == pSieveEnd )
                     goto end;
             }
             bit_it pSieve = sieve.begin();
             if( size_t m = p * p; p < sqr )
                 if( p >= BITS ) [[likely]]
                 {
                     word_t mask = rotl( (word_t)-2, m % BITS );
                     do
                         pSieve[m / BITS].w &= mask,
                         mask = rotl( mask, (int)p % BITS ),
                         m += p;
                     while( m < end );
                 }
                 else
                 {
                     pSieve += m / BITS;
                     word_t mask = rotl( (word_t)-2, m % BITS );
                     do
                     {
                         word_t punch = -1, oldMask;
                         do
                             punch &= mask,
                             oldMask = mask,
                             mask = rotl( mask, (int)p );
                         while( mask < oldMask );
                         pSieve->w &= punch;
                     } while( ++pSieve != pSieveEnd );
                 }
         }
     end:
         if( ofs.is_open() )
             print();
         return EXIT_SUCCESS;
     }
     catch( bad_alloc const & )
     {
         cout << "out of memory" << endl;
     }
     catch( ios_base::failure const & )
     {
         cout << "I/O error" << endl;
     }
     catch( system_error const &se )
     {
         cout << se.what() << endl;
     }
}

[toc] | [prev] | [next] | [standalone]


#91474

FromBonita Montero <Bonita.Montero@gmail.com>
Date2023-08-30 19:02 +0200
Message-ID<ucnsmq$2qeb0$1@dont-email.me>
In reply to#91467
Am 29.08.2023 um 20:34 schrieb Bonita Montero:

> size_t g = countr_zero( pSieve->w & (word_t)-1 << p %  BITS );

   size_t g = countr_zero( (word_t)(pSieve->w & (word_t)-1 << p %  BITS) );


OMG, an integral promotion error.
If word_t is defined < unsigned the results are wrong.

[toc] | [prev] | [standalone]


Page 8 of 8 — ← Prev page 1 2 3 4 5 6 7 [8]

Back to top | Article view | comp.lang.c++


csiph-web