Talk:First power of 2 that has leading decimal digits of 12: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 25: Line 25:
<pre> 779 485 485 1651 485 485 1166 485 485 1166 485 485 1166 485 485 1166 485 485 1166 485 485 1166 485 485 1651 </pre>
<pre> 779 485 485 1651 485 485 1166 485 485 1166 485 485 1166 485 485 1166 485 485 1166 485 485 1166 485 485 1651 </pre>
--[[User Horst.h|Horst.h]]12:14, 20 January 2020 (UTC)~
--[[User Horst.h|Horst.h]]12:14, 20 January 2020 (UTC)~

:In my first (abortive) attempt at this I'd noticed that (for a prefix of "123"), after an initial {90, 289} pair, subsequent differences were either 196, 289 or 485 (= 289 + 196). Moreover, 289 was always followed by 196 but 196 could be followed by either 289 or 485, and 485 could be followed by either 196 or 485 again. I wasn't able to discern any deeper pattern than this. So I'd ended up with this (Go) code:

:<lang go>func p123(n int) uint {
power, shift := uint(0), uint(90)
one := big.NewInt(1)
temp := new(big.Int)
for count := 0; count < n; count++ {
power += shift
switch shift {
case 90:
shift = 289
case 289:
shift = 196
case 196:
shift = 289
temp.Set(one)
temp.Lsh(temp, power+289)
if !strings.HasPrefix(temp.String(), "123") {
shift = 485
}
case 485:
shift = 196
temp.Set(one)
temp.Lsh(temp, power+196)
if !strings.HasPrefix(temp.String(), "123") {
shift = 485
}
}
}
return power
}</lang>

:But, it turned out that this was very slow (4.5 minutes) as opposed to your log based approach at around 40 seconds or so. In his Perl 6 solution, I notice that Thundergnat has managed to combine your log approach with searching for patterns in the differences:

:<lang perl 6>my @startswith123 = lazy gather loop {
state $pre = '1.23';
state $count = 0;
state $this = 0;
given $this {
when 196 {
$this = 289;
my \n = $count + $this;
$this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre;
}
when 485 {
$this = 196;
my \n = $count + $this;
$this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre;
}
when 289 { $this = 196 }
when 90 { $this = 289 }
when 0 { $this = 90 }
}
take $count += $this;
}</lang>

:though, given the substantial speed-up you've already achieved with your 'alternative' version, I don't know whether considering patterns as well is going to give a worthwhile improvement. --[[User:PureFox|PureFox]] ([[User talk:PureFox|talk]]) 14:47, 20 January 2020 (UTC)

Revision as of 14:47, 20 January 2020

License check

We seem to be following the license from the original site, (although I couldn't find the problem number to add a link).
- Note: I am not the uthor of thie task, just checking the license. --Paddy3118 (talk) 22:51, 15 January 2020 (UTC)

I've found the problem number which is 686. --PureFox (talk) 23:21, 15 January 2020 (UTC)
I was just about to add the (home page) information.   I had originally phrased the problem much differently, but then it seemed to be more confusing and somewhat unclear, so I went with the original wording and phrasing that someone had paraphrased.   I tried to find the problem on that website, but I couldn't find   (I must have mis-remembered some keyword/keywords because I couldn't locate it via my search engine, other than it was on that site.     -- Gerard Schildberger (talk) 23:29, 15 January 2020 (UTC)
Your RC title gives more info 👍 --Paddy3118 (talk) 12:25, 16 January 2020 (UTC)

significance of a given   p   value

I wonder what the significance is of saying that: "You are also given that p(123,45)=12710."? It's not as if it's going to take a long time to calculate whatever method is used. --PureFox (talk) 23:49, 15 January 2020 (UTC)
The reasoning behind that (I believe) was to give the programmer an answer so that it could be used for verification of their programming solution to check if their calculation was working correctly for a "modest" (or easy) expression/formula.     -- Gerard Schildberger (talk) 00:07, 16 January 2020 (UTC)
Yeah, makes sense I guess. I'd been looking for a deeper meaning but couldn't think of any. --PureFox (talk) 00:12, 16 January 2020 (UTC)

is there an easy way to get those delta 2^i (delta = i-Last_i ) ?

searching for '123' there is a double 485 Delta

   90  289  196  289  196  485  196  289  196  289  196  289  196  485  196  289  196  289  196  289  196  485  196  289  196  289  196  289  196  485  485  196 

searching for '317' ( not the smallest at start, 1651 seldom )

  779  485  485 1651  485  485 1166  485  485 1166  485  485 1166  485  485 1166  485  485 1166  485  485 1166  485  485 1651 

--Horst.h12:14, 20 January 2020 (UTC)~

In my first (abortive) attempt at this I'd noticed that (for a prefix of "123"), after an initial {90, 289} pair, subsequent differences were either 196, 289 or 485 (= 289 + 196). Moreover, 289 was always followed by 196 but 196 could be followed by either 289 or 485, and 485 could be followed by either 196 or 485 again. I wasn't able to discern any deeper pattern than this. So I'd ended up with this (Go) code:
<lang go>func p123(n int) uint {
   power, shift := uint(0), uint(90)
   one := big.NewInt(1)
   temp := new(big.Int)
   for count := 0; count < n; count++ {
       power += shift
       switch shift {
       case 90:
           shift = 289
       case 289:
           shift = 196
       case 196:
           shift = 289
           temp.Set(one)
           temp.Lsh(temp, power+289)
           if !strings.HasPrefix(temp.String(), "123") {
               shift = 485
           }
       case 485:
           shift = 196
           temp.Set(one)
           temp.Lsh(temp, power+196)
           if !strings.HasPrefix(temp.String(), "123") {
               shift = 485
           }
       }
   }
   return power

}</lang>

But, it turned out that this was very slow (4.5 minutes) as opposed to your log based approach at around 40 seconds or so. In his Perl 6 solution, I notice that Thundergnat has managed to combine your log approach with searching for patterns in the differences:
<lang perl 6>my @startswith123 = lazy gather loop {
   state $pre   = '1.23';
   state $count = 0;
   state $this  = 0;
   given $this {
       when 196 {
           $this = 289;
           my \n = $count + $this;
           $this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre;
       }
       when 485 {
           $this = 196;
           my \n = $count + $this;
           $this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre;
       }
       when 289 { $this = 196 }
       when 90  { $this = 289 }
       when 0   { $this = 90  }
   }
   take $count += $this;

}</lang>

though, given the substantial speed-up you've already achieved with your 'alternative' version, I don't know whether considering patterns as well is going to give a worthwhile improvement. --PureFox (talk) 14:47, 20 January 2020 (UTC)