Jump to content

PHP help

xampi

So I want to create a table with php and html that outputs a 10x10 table with numbers from 0 to 99 and differentiating prime numbers:

The problem is that every time it checks prime numbers from 0 to 99 it prints it in every '<tr>'. Ty in advance for the help!

 

<html>

 <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

</head>

<body>

   <?php  

        echo "<table>";

        for($i=0$i < 10$i++){

            echo "<tr>";

            for($x=0$x < 10$x++){

                for($i=0;$i<=99;$i++){

                    $counter = 0; 

                    for($j=1;$j<=$i;$j++){

                        if($i % $j==0){

                            $counter++;

                        }

                    }

                    if($counter==2){

                        echo "<td style="background-color: yellow;">".$i."</td>";

                    }else{

                        echo "<td style="background-color: blue>".$i."</td>";

                    }

                }

            echo "</tr>";

            }

        }

        echo "</table>";

    ?>  

</body>

</html>

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, xampi said:

if($counter==2){
	echo "<td style="background-color: yellow;">".$i."</td>";
}else{
	echo "<td style="background-color: blue>".$i."</td>";
}

    

I think this is where the code goes wrong, as you are using double quotes for the echo and for the HTML tags, so the code doesn't know how to handle them (plus I am pretty sure there are some issues with the code in general, see the ; after yellow, the lack of a double quote after blue)

Either use single quotes for the echo or escape the HTML's quotes.

// Current:
if($counter == 2){
	echo "<td style="background-color: yellow;">".$i."</td>";
}else{
	echo "<td style="background-color: blue>".$i."</td>";
}

// New, single quotes:
if($counter == 2){
	echo '<td style="background-color: yellow">'$i.'/td>'
}else{
	echo '<td style="background-color: blue">'.$i.'</td>';
}

// New, escaped HTML quotes:
if($counter == 2){
	echo "<td style=\"background-color: yellow\">".$i."</td>";
}else{
	echo "<td style=\"background-color: blue\">".$i."</td>";
}

 

Be sure, when debugging code, to split it in different parts.

First check if the for loops work, then the HTML code, etc. so you know where to look for issues.

 

P.S. I really don't know PHP, so I may have gotten some thing wrong.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Ty for helping @minibois!

 

image.thumb.png.c4bd71b1bbc03e0d5a04c3da46486074.png

 

Sadly it still echo's like this.

 

Here an example I would like to output:

 

image.png.7e8b6d4365b8afb251a4e853f3d97791.png

Link to comment
Share on other sites

Link to post
Share on other sites

First, you should make a function which determines if a number is prime or not. 

Second, maybe use the short version of if-then-else 

 

Example: 

 

<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
   <?php  

function is_prime($number) {
	if ($number <2) return false; 
    	// overkill, you could just use sqrt($number) and it will still work
	for ($i = 2; $i <= intval(floor(sqrt($number))); $i++) { 
		if ($number % $i == 0) return false; // returns as soon as it divides with a number so faster
	} 
	return true; 
}

	echo "<table>";
	for($y=0; $y < 10; $y++){
		echo "<tr>";
		for($x=0; $x < 10; $x++){
			$number = $y*10 + $x;
			$prime = is_prime($number);
			echo '<td style="background-color: '. (($prime==true) ? 'yellow' : 'blue') .';">'.$number.'</td>';
			}
		echo "</tr>";
	}
	echo "</table>";

    ?>  

</body>
</html>

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, mariushm said:

First, you should make a function which determines if a number is prime or not. 

Second, maybe use the short version of if-then-else 

 

Example: 

 


<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
   <?php  

function is_prime($number) {
	if ($number <2) return false; 
    	// overkill, you could just use sqrt($number) and it will still work
	for ($i = 2; $i <= intval(floor(sqrt($number))); $i++) { 
		if ($number % $i == 0) return false; // returns as soon as it divides with a number so faster
	} 
	return true; 
}

	echo "<table>";
	for($y=0; $y < 10; $y++){
		echo "<tr>";
		for($x=0; $x < 10; $x++){
			$number = $y*10 + $x;
			$prime = is_prime($number);
			echo '<td style="background-color: '. (($prime==true) ? 'yellow' : 'blue') .';">'.$number.'</td>';
			}
		echo "</tr>";
	}
	echo "</table>";

    ?>  

</body>
</html>

 

Is there a way to do it using just loops, variables and IF's?

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, xampi said:

image.thumb.png.c4bd71b1bbc03e0d5a04c3da46486074.png

Sadly it still echo's like this.

@mariushm's code seems like it should work for you, plus it's also a lot neater and easier to read.

But just for completion sake, this is why it outputs all the 99 numbers on the same row:

for($i=0; $i < 10; $i++){
    echo "<tr>";
        for($x=0; $x < 10; $x++){
            for($i=0;$i<=99;$i++){
                // Prime number logic, snipped out.
            }
    echo "</tr>";
}

Essentially what you're saying here is:

- make 10 rows

- do something 10 times on each row

- do something 99 times on each row

- close the row (of course 10 times)

 

That was sort of what I mentioned earlier with debugging your code, isolate the different parts and think about they should do and what they do now.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Here's without functions

 

<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
   <?php  

echo "<table>";
	for($y=0; $y < 10; $y++){
		echo "<tr>";
		for($x=0; $x < 10; $x++){
			$number = $y*10 + $x;

			// default to assuming it's prime
			$prime = true;
			// now do the actual checking
			if ($number <2) { 
				$prime = false;
			} else {
              			$limit = intval(floor(sqrt($number)));
				for ($counter = 2; $counter <= $limit; $counter++) { 
					if ($number % $counter == 0) { 
						$prime = false; 
						// make counter super high, so we exit for right away instead of continuing all the way to sqrt(number)
						$counter = $number; 
					}
				}	 
			}


			echo '<td style="background-color: '. (($prime==true) ? 'yellow' : 'blue') .';">'.$number.'</td>';
			}
		echo "</tr>";
	}
	echo "</table>";

    ?>  

</body>
</html>

 

 

The prime checking is optimized using square root of the number, because you only need to check up to that point. 

Also, once we find the number divides by something, there's no point testing all the other numbers, we can quit early because we know the number is not a prime. That's why I change the counter variable to something so high, it's obvious the condition  number < integer part of square root of number  will fail, so for will exit. 

 

I'm using floor to get the number rounded down  ... ex sqrt(2) = 1.41  ... floor (1.41) is 1 , and intval(1)  is 1 ... intval gets the integer part of a fractional number. 

In theory it's faster code because in the for loop the counter variable would have to be converted to float every time, to be compared against square root of the number and see if the condition is true or not. 

I use intval to get the integer part of that, and then inside the for, we compare an integer to another integer which is much faster. 

 

In practice, in this example, it's overkill, because with modern processors, we're talking about a difference of a few nanoseconds, hardly worth talking about. 

 

using sqrt(number) does save a lot of computations... because you test 100 numbers, so you'd have 0+1+2+3+4+...100 divisions in the for loops without sqrt...  a bit under 10000 divisions. With sqrt you have much less. 

Again, for 100 numbers, it's a difference of nanoseconds but if you had to compute first 100k prime numbers, it would a lot of time saved.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the help guys. It was as easy as closing the 'tr' and open it back again after a counter of 10. Here the solution:

    <?php  
        echo "<table>";
            echo "<tr>";
                for($i=0;$i<=99;$i++){
                    $counter = 0
                    if(($i % 10) == 0){
                        echo "</tr><tr>";
                    }
                    for($j=1;$j<=$i;$j++){
                        if($i % $j== 0){
                            $counter++;
                        }
                    }
                    if($counter == 2){
                        echo "<td style='background-color: yellow'>".$i."</td>";
                    }else{
                        echo "<td style='background-color: blue'>".$i."</td>";
                    }
                }
                echo "</tr>";
        echo "</table>";
    ?>  
Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, mariushm said:

Here's without functions

 


<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
   <?php  

echo "<table>";
	for($y=0; $y < 10; $y++){
		echo "<tr>";
		for($x=0; $x < 10; $x++){
			$number = $y*10 + $x;

			// default to assuming it's prime
			$prime = true;
			// now do the actual checking
			if ($number <2) { 
				$prime = false;
			} else {
				for ($counter = 2; $counter <= intval(floor(sqrt($number))); $counter++) { 
					if ($number % $counter == 0) { 
						$prime = false; 
						// make counter super high, so we exit for right away instead of continuing all the way to sqrt(number)
						$counter = $number; 
					}
				}	 
			}


			echo '<td style="background-color: '. (($prime==true) ? 'yellow' : 'blue') .';">'.$number.'</td>';
			}
		echo "</tr>";
	}
	echo "</table>";

    ?>  

</body>
</html>

 

Wow. Ty for helping!

Link to comment
Share on other sites

Link to post
Share on other sites

I edited my previous post, maybe you'll want to see the comments. 

Also, if you have other problems, ask and at least I'll try to help when i have time.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sakuriru said:

Darn I was going to flex my newly minted php skills.

 

I strongly recommend storing your prime numbers as a global variable $prime_numbers. It should also be noted that a sieve is considered to be the fastest method of producing primes (not exactly the best as far as memory is concerned, though).

Why? Your just increasing the memory footprint, code execution time, and adding extra steps for no real reason. The numbers need calculated regardless and they are not reused.

Link to comment
Share on other sites

Link to post
Share on other sites

Well, technically you can use a bit to store if a number is prime or not.  So, a 64 bit number would be enough to hold the information about which of the first 64 numbers are prime. 

You can use an array of 64 bit numbers to store info about whether  64xn numbers are prime 

The example below calculates the first 640 prime numbers and puts them into 10 64 bit numbers... using 80 bytes of memory  :

 

you can determine if a bit is set to 1 or not using   $bit_set = $number & (1<< $n)   where $n = 0..63

 

   <?php  

function is_prime($number) {
	if ($number <2) return false; 
	for ($i = 2; $i <= intval(floor(sqrt($number))); $i++) { 
		if ($number % $i == 0) return false; 
	} 
	return true; 
}
$number = '';
for ($i=0;$i<640;$i++) {
$number = $number . ((is_prime($i)==true) ? '1' : '0');
if (strlen($number)==64) {
	echo "number in binary = $number <br/>";
	echo "number in decimal = ".base_convert($number,2,10).' <br/>';
	echo "number in hexadecimal = 0x".base_convert($number,2,16).'<br/>';
	$number = '';
}

}
?>
number in binary = 0011010100010100010100010000010100000100010100010000010000010100
number in decimal = 3824771065533498388
number in hexadecimal = 0x3514510504510414

number in binary = 0001000101000001000100000100000001000101000101000100000000000001
number in decimal = 1243292840353873921
number in hexadecimal = 0x1141104045144001

number in binary = 0001000001010000000001010000010000010001000001000001010000000001
number in decimal = 1175445017767187457
number in hexadecimal = 0x1050050411041401

number in binary = 0100010100000000000100000000000100010100010000010100000000010000
number in decimal = 4971991585437859856
number in hexadecimal = 0x4500100114414010

number in binary = 0100000100000101000001000101000000000100000000000001000101000100
number in decimal = 4685155729059877188
number in hexadecimal = 0x4105045004001144

number in binary = 0000000000010000010000000001010001000001000000010000010000010001
number in decimal = 4574055361479697
number in hexadecimal = 0x10401441010411

number in binary = 0000010000000100010000000100000000010100000000010100000100010000
number in decimal = 289426920016265488
number in hexadecimal = 0x404404014014110

number in binary = 0100000001000101000100000000000100000001000100000001000100000100
number in decimal = 4631125388319265028
number in hexadecimal = 0x4045100101101104

number in binary = 0000000001010000000000000000010000010000000001000001000001010000
number in decimal = 22518015585423440
number in hexadecimal = 0x50000410041050

number in binary = 0100000000010000010000010100000100000100010100000000000100000000
number in decimal = 4616261365555790080
number in hexadecimal = 0x4010414104500100

 

if you want the first million prime numbers or something like that, you can get into compressing such sequences, for example using run-length-encoding.

 

For example let's take the last number we determined 

 

number in binary = 0100000000010000010000010100000100000100010100000000000100000000
number in decimal = 4616261365555790080
number in hexadecimal = 0x4010414104500100

 

You can make some rules: 

 

* 4 bits of data are used to signal how many 0 bits you have... this means you have between 0 and 14  of 0's possible. 

* if the value is 15, that means more than 15 0's so add the value to the one already measured. 

 

So the binary number above can be encoded as 

1 9 5 5 1 5 5 3  1 11 8

 

So you've used 11 x 4 bit chunks, so 5.5 bytes to compress 8 bytes.

 

You could also go into building a dictionary, like making your 1 million 0s and 1s and then calculating how many times 101 shows up, how many times 00000 shows up and so on, and you can give the least number of bits to the sequence with most occurrences and so on... and you've invented dictionary compression (lz77, zip, huffman tables etc) 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×