Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

array

Posted on 2011-09-23
7
Medium Priority
?
336 Views
Last Modified: 2012-06-27
Fill array from 1 to 512.  Echo only values that are powers of 2
0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 13

Accepted Solution

by:
themrrobert earned 2000 total points
ID: 36589222
Do you really need to fill an array then?

Try this:

for($x=1;$x<=512;$x++)
  if(intval(sqrt($x))==sqrt($x)) //match
    echo "$x is a power of 2<br />";

this outputs:

1 is a power of 2
4 is a power of 2
9 is a power of 2
16 is a power of 2
25 is a power of 2
36 is a power of 2
49 is a power of 2
64 is a power of 2
81 is a power of 2
100 is a power of 2
121 is a power of 2
144 is a power of 2
169 is a power of 2
196 is a power of 2
225 is a power of 2
256 is a power of 2
289 is a power of 2
324 is a power of 2
361 is a power of 2
400 is a power of 2
441 is a power of 2
484 is a power of 2

Now if you want to know if 2^x = (array) that's a different thing altogether (this will find values like 512, 256, etc) ill get you the code in a sec
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36589261
for($x=0;$x<10;$x++)
  echo "2 to the x($x) power = " . pow(2, $x)."<br />";

results:

2 to the x(0) power = 1
2 to the x(1) power = 2
2 to the x(2) power = 4
2 to the x(3) power = 8
2 to the x(4) power = 16
2 to the x(5) power = 32
2 to the x(6) power = 64
2 to the x(7) power = 128
2 to the x(8) power = 256
2 to the x(9) power = 512
0
 
LVL 8

Expert Comment

by:ropenner
ID: 36589294
<?php
$power = 2;

// fill an array with numbers
for ($i=1;$i < 513; $i++) {
      $joe[] = $i;
      $check = log($i) / log($power);
      
      // if $check is an integer then it is a power of 2
      if ($check == intval($check)) {
            echo $i ."<BR>\n";
      }
}
//var_dump($joe);
?>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ropenner
ID: 36589321
sounds like a school problem, but the array is extraneous unless the check must be done while filling the array.

2^x = y  (2 to the power of x)
x log 2 = log y
x = (log y) / (log 2)

Since the last line is true then all integer values of x are powers of 2 for the input y = 1..512
0
 

Author Comment

by:rgb192
ID: 36589326
I get all values on code sample 1
0
 
LVL 2

Expert Comment

by:shdwmage
ID: 36589339
I'm with themrrobert on this one.  You didn't give us enough information.

If you really wanted it in an array, you could take robert's example and modify it.

$array_variable = NULL;
for($x=0;$x<10;$x++){
$array_variable .= pow(2,$x) . ",";
}
$array = explode(",",$array_variable);

Open in new window


Note that this method will leave a blank record at the end you will need to account for.
0
 

Author Closing Comment

by:rgb192
ID: 36589502
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question