Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » powers of 2 and 3

This thread is locked; no one can reply to it. rss feed Print
 1   2 
powers of 2 and 3
William Labbett
Member #4,486
March 2004
avatar

Program Output

Quote:

4 2
9 3
8 2
16 2
27 3
32 2
81 3
64 2
128 2
243 3
256 2
729 3
512 2
1024 2
2187 3
2048 2
4096 2
6561 3
8192 2
19683 3
16384 2
32768 2
59049 3
65536 2
177147 3
131072 2
262144 2
531441 3
524288 2
1048576 2
1594323 3
2097152 2
4782969 3
4194304 2
8388608 2
14348907 3
16777216 2

The above numbers are the powers of 2 and 3 in order. Hopefully you get my
meaning.

The ordering starts

2, 3, 2, 2, 3

By which is meant, there's a power of 2, a power of 3, 2 powers of 2 and then a power of 3.

and repeats like that.

Then it's 2, 2, 3, 2, 3.

Anyway, there's 2 cases where it goes 2, 2, 3, 2, 2, where the 3's are 2187 and 531441

I wonder what power of 3, is the next one in the pattern, 2, 2, 3, 2, 2 ?

Just thought, that was interesting and wondering if anyone's got any thoughts on the matter ?

Dustin Dettmer
Member #3,935
October 2003
avatar

Sort of reminds me of prime number type patterns. I wonder if this could apply to encryption techniques.

Bruce Perry
Member #270
April 2000

Yep, it's interesting :)

They're not in order though. You've got 9 before 8, then 81 before 64, and it happens a number more times. ;)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
The brxybrytl has you.

bamccaig
Member #7,536
July 2006
avatar

A Perl one-liner to fix it. ;) (Using STDIN and STDOUT)

perl -E '$,=" ";while(<>){chomp;push @d,[split];}@d=sort {$a->[0]<=>$b->[0]} @d;say @$_ for(@d)'

Evert
Member #794
November 2000
avatar

bamccaig said:

A Perl one-liner to fix it.

sort -n.
Less typing.

anonymous
Member #8025
November 2006

The first 10 ones should be

3^ 7 = 2187
3^12 = 531441
3^19 = 1162261467
3^24 = 282429536481
3^31 = 617673396283947
3^36 = 150094635296999121
3^43 = 328256967394537077627
3^48 = 79766443076872509863361
3^53 = 19383245667680019896796723
3^60 = 42391158275216203514294433201

William Labbett
Member #4,486
March 2004
avatar

I didn't notice I made a mistake. The program took me 5 minutes to write.

Here's the code :

#SelectExpand
1#include <stdlib.h> 2#include <stdio.h> 3 4 5 6 7 8 9int power( int number, int power) 10{ 11 int result = 1; 12 13 while(power > 0) 14 { 15 result *= number; 16 --power; 17 } 18 19 return result; 20} 21 22 23int main() 24{ 25 int exponent; 26 int i; 27 long int powers_of_three[1000]; 28 long int powers_of_two[1000]; 29 30 FILE *f = fopen("twos_and_threes.txt", "w"); 31 32 for(i = 0; i < 1000; ++i) 33 { 34 powers_of_three[i] = power(3, i); 35 powers_of_two[i] = power(2, i); 36 } 37 38 for(i = 0; i < 1000; ++i) 39 { 40 printf("_ _ _%4d", i); 41 } 42 43 int i2, i3; 44 45 i2 = i3 = 1; 46 47 for(i = 0; i < 10000000; ++i) 48 { 49 50 51 if( powers_of_two[i2] < i ) 52 { 53 ++i2; 54 fprintf(f, "%d %d\n", powers_of_two[i2], 2); 55 56 } 57 58 if(powers_of_three[i3] < i ) 59 { 60 ++i3; 61 fprintf(f, "%d %d\n", powers_of_three[i3], 3); 62 } 63 64 if(i2 == 999 || i3 == 999) 65 { 66 break; 67 } 68 } 69 70 fclose(f); 71 72 return 0; 73}

Thanks anonymouse for working those out.

Karadoc ~~
Member #2,749
September 2002
avatar

that's a weird way to sort them at the end.
I'd recommend something like this:

int i = 1, j = 1;
while (i < 1000 || j < 1000)
{
  if (j >= 1000 || (i < 1000 && powers_of_two[i] <= powers_of_three[j]))
  {
    fprintf(f, "%d       %d\n", powers_of_two[i], 2);
    i++;
  }
  else
  {
    fprintf(f, "%d       %d\n", powers_of_three[j], 3);
    j++;
  }
}

-----------

William Labbett
Member #4,486
March 2004
avatar

Thanks for that Karadoc. Copied and pasted.

Tobias Dammers
Member #2,604
August 2002
avatar

Lemme try in Haskell...

module Main
where

main = do
    let merge (x:xs) (y:ys) =
            if x < y
                then x:(merge xs (y:ys))
                else y:(merge (x:xs) ys)
        p2 = map (2 ^) [1..]
        p3 = map (3 ^) [1..]
        pm = merge p2 p3
        pp = concat $ map ((++ "\n") . show) pm
    putStrLn pp

This one spits out all the powers of two and three, in order, until you cancel out.

If you want to limit the output to 100 lines, you'd change one line to:

pm = take 100 $ merge p2 p3

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

William Labbett
Member #4,486
March 2004
avatar

Quote:

3^ 7 = 2187
3^12 = 531441
3^19 = 1162261467
3^24 = 282429536481
3^31 = 617673396283947
3^36 = 150094635296999121
3^43 = 328256967394537077627
3^48 = 79766443076872509863361
3^53 = 19383245667680019896796723
3^60 = 42391158275216203514294433201

So the exponents go

7 12 19 24 31 36 43 48 53 60

seems to be some kind of pattern there :

+5 +7 +5 +7 +5 +7 +5 +5 +7

hhhmmmm

Matthew Leverton
Supreme Loser
January 1999
avatar

From where do you get your number crunching ideas?

William Labbett
Member #4,486
March 2004
avatar

Read a book a long time ago called Fermat's Last Theorem, by Simon Singh which I borrowed from someone I know who has a degree in mathematics.

I got wondering about an unsolved problem mentioned in the book, which is about whether or not there exist any odd perfect numbers.

Over the years I went from one unsolved problem to another.

Are there infinitely many Mersenne Primes ?
Are there infinitely many twin primes ?

There's loads of unsolved problems.

My brain has evolved in a strange way consequently and I think within the context of
a background of things I've thought about before for a long time.

So it all begins with books, but I think and so ultimately my ideas come from my curiousity for mathematics.

It's not something I can honestly recommend since it could be seen that I've wasted a lot of time as I haven't solved any of the problems, but I've learnt a lot along the way.

Whether what drives me is a love for mathematics or a desire for fame or a mixture or something else entirely I'm not sure.

Hope I've answered your question. I tried my best.

Matthew Leverton
Supreme Loser
January 1999
avatar

Yeah, I was basically just wondering if you were actually trying to solve problems or if you just saw patterns and got curious about them.

J-Gamer
Member #12,491
January 2011

@William: You may find this to be interesting :)

My website: J-Games
" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

anonymous
Member #8025
November 2006

Apparently my earlier results are all wrong. I made a similar mistake like you did and was happy to see that the first two results matched. Such sequences happen but at different powers.

See the corresponding exponents of 3 that match to the powers of 2: http://ideone.com/TGkO0

When you see a sequence where two consequtive whole parts are repeated twice, such as

8 5.04743802857
9 5.67836778214
10 6.30929753571
11 6.94022728929

then this is the pattern you are interested in. It means that

35 < 28 < 29 < 36 < 210 < 211 < 37

And since the exponent of 3 grows linearly, and 0.630929753571 * 3 = 1.892789260713 < 2, such pattern will always occur occasionally. (However, a pattern containing more than 2 powers of 2 between powers of 3 can't occur.)

Arthur Kalliokoski
Member #5,540
February 2005
avatar

I got bored with the current project, so I thought I'd try it. It sorts on the go.

#SelectExpand
1#include <stdio.h> 2#define MAXRESULTS 120 3 4typedef struct 5{ 6 unsigned long long source; 7 unsigned long long power; 8}powerrecord_t; 9 10powerrecord_t results[MAXRESULTS]; 11 12int main(void) 13{ 14 int i; 15 16 unsigned long long twos = 2; 17 unsigned long long threes = 3; 18 19 unsigned long long limit = 0x04000000000000000; 20 21 int powindex = 0; 22 23 int whichlimit = 0; 24 25 twos *= 2; //prime the pump 26 threes *= 3; 27 28 while(whichlimit == 0) 29 { 30 while(twos < threes) 31 { 32 if(twos > limit) 33 { 34 whichlimit = 1; 35 break; 36 } 37 38 results[powindex].source = 2; 39 results[powindex].power = twos; 40 powindex++; 41 if(powindex == MAXRESULTS) 42 { 43 whichlimit = 2; 44 break; 45 } 46 twos *= 2; 47 } 48 49 while(threes < twos) 50 { 51 if(threes > limit) 52 { 53 whichlimit = 1; 54 break; 55 } 56 57 results[powindex].source = 3; 58 results[powindex].power = threes; 59 powindex++; 60 if(powindex == MAXRESULTS) 61 { 62 whichlimit = 2; 63 break; 64 } 65 threes *= 3; 66 } 67 } 68 69 for(i=0;i<powindex;i++) 70 printf("%llu %llu\n",results[i].source,results[i].power); 71 72 if(whichlimit == 1) 73 printf("Ran into result size limit\n"); 74 else if(whichlimit == 2) 75 printf("Ran into array size limit\n"); 76 else 77 printf("Internal error\n"); 78 return 0; 79}

I'll try it with my bignum library next.

I really admire the U.S. Constitution. It's so much better than what we have now.

Oscar Giner
Member #2,207
April 2002
avatar

Isn't that overcomplicated?

Just:

#SelectExpand
1#include <stdio.h> 2 3#define N 40 4 5int main() 6{ 7 unsigned pow2 = 2; 8 unsigned pow3 = 3; 9 10 for(unsigned i=0; i<N; ++i) 11 { 12 if (pow3 < pow2) 13 { 14 printf ("%10d - 3\n", pow3); 15 pow3 *= 3; 16 } 17 else 18 { 19 printf ("%10d - 2\n", pow2); 20 pow2 *= 2; 21 } 22 } 23}

output:

         2 - 2
         3 - 3
         4 - 2
         8 - 2
         9 - 3
        16 - 2
        27 - 3
        32 - 2
        64 - 2
        81 - 3
       128 - 2
       243 - 3
       256 - 2
       512 - 2
       729 - 3
      1024 - 2
      2048 - 2
      2187 - 3
      4096 - 2
      6561 - 3
      8192 - 2
     16384 - 2
     19683 - 3
     32768 - 2
     59049 - 3
     65536 - 2
    131072 - 2
    177147 - 3
    262144 - 2
    524288 - 2
    531441 - 3
   1048576 - 2
   1594323 - 3
   2097152 - 2
   4194304 - 2
   4782969 - 3
   8388608 - 2
  14348907 - 3
  16777216 - 2
  33554432 - 2

Arthur Kalliokoski
Member #5,540
February 2005
avatar

I'm doing the bignum version now, the version I posted was more or less a testbed with error checking.

[EDIT]

I've forgotten how to use these bignums, a week ago I made a little prog for a bet to determine how far away the horizon should be for a man with his eyes 5.5 feet off the ground and it took me like two hours :\

[EDIT2]

With an integer size of a relatively svelte 1032 bytes each, it ran into the limit after outputting 15.5MB of text. I put the last 5 lines into the paperclip as a zip file. I suppose I could skip printing the powers and only print which of 2 or 3 was next if anybody wants it.

[EDIT4]

With only 4GB of memory, I managed to coax the first 133492 results and put it in the paperclip as "t3.txt.zip". It took 4.134 seconds to calculate. I'd get a few thousand more lines if I played with the intsize and maxlimit.

[EDIT5]

I looked at Oscar's version again and see that he doesn't store the numbers :-X
I'll let it go at that, 133492 results is enough to study.

I really admire the U.S. Constitution. It's so much better than what we have now.

anonymous
Member #8025
November 2006

I looked at Oscar's version again and see that he doesn't store the numbers

It's simple and sweet, except there is no place to process the numbers (without storing them). For counting powers of two between powers of three, perhaps an inner loop?

_2, _3 = 2,3
for i in range(100):
   c = 0
   while _2 < _3:
     _2 *= 2
     c += 1
   print "%d powers of two in range 3^%d...3^%d" % (c, i, i+1)
   _3 *= 3

William Labbett
Member #4,486
March 2004
avatar

Sorry guys. Only just got back.

The file results.txt.zip is just one long number. Shouldn't there be spaces in there.

What's the file all about ?

Arthur Kalliokoski
Member #5,540
February 2005
avatar

The file results.txt.zip is just one long number. Shouldn't there be spaces in there.

It has the *nix line feed only '\n', not the DOS '\r\n'. Maybe you have an old Allegro utod.exe? Or view it with a different editor.

I really admire the U.S. Constitution. It's so much better than what we have now.

bamccaig
Member #7,536
July 2006
avatar

Perl one-liner -> LF to CRLF[1][2]:

perl "-MFile::Slurp qw/edit_file/" -E "edit_file { s/([^\r])\n/$1\r\n/gm } $ARGV[0], { binmode => q{:raw} }" path\to\file

References

  1. Unscrupulously tested.
  2. Windows shell quotes and path separators; substitute " with ' and \ with / in *nix.
Tobias Dammers
Member #2,604
August 2002
avatar

Shell one-liner:

$ fromdos path/to/file

Or you could do it with a tiny bit of sed, or good old tr:

$ mv path/to/file path/to/file~ && tr -d '\r' <path/to/file~ >path/to/file

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Karadoc ~~
Member #2,749
September 2002
avatar

I like the perl one-liner things - but mostly because I think it's funny (as in humorous).

Maybe I should install perl so that maybe one day I can swing across the room on a rope and miraculously solve problems.

-----------

 1   2 


Go to: