PHP - Get Prime Numbers And Print
hi , how to get the prime numbers of the positive integer input and print them?
here's what i have at the moment function getPrime($num){ if((int)$num == $num && (int)$num > 0){ for($i = 2; $i < $num; $i++){ for($j = 2; $j < $i; $j++){ if($i % 2 == 1){ echo $i.'<br />'; } } } } } displayPrime(10); the ouput should be Code: [Select] 2,3,5,7 but the output apparently, is Code: [Select] 3 5 5 5 7 7 7 7 7 9 9 9 9 9 9 9 how to solve this? Similar TutorialsSo 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! It outputs a check of primer numbers from 0 to 99 every tr and i want it to be a single check from 0 to 99 using 10 tr <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> Hello, I am fairly new to PHP and I found some php code to print an array of numbers that create a combination - however, its slams all the numbers together without a space or a comma separator. How can I separate the numbers in this array with the given code? Ive attached a photo of the code.
Hi, I need to sort variables in groups of up to 15 and put it in an array. For example: $exstract['center_tabOpBody_0'] =5 $exstract['center_tabOpBody_1'] =6 $exstract['center_tabOpBody_2'] =8 $exstract['center_tabOpBody_3'] =1 Should yield: ARRAY( = center_tabOpBody_1,center_tabOpBody_2,center_tabOpBody_3 // <-----15 [1] = center_tabOpBody_0 //<----5 ) Is there some simple function do do the "efficiency" sort? Thanks, Vadim can you have something like.. echo $whatever; this putput 1.2 can you split this number so the first integer will be in $val"(1)" or whatever and the second integer after the "." will be in $val2"(2)" Hi Guys
I have a regex conundrum that at first I thought would be very easy but then appears to be quite hard :/
I want to match a string with a regex where the regex can only pass if it contains letters and numbers (nothing else) and it has to contain at least one of each.
I've been trying out lookaheads but just can't get it right.
Any help is appreciated.
Drongo
is this a proper way of entering this? Code: [Select] $result = mysql_query("SELECT * FROM jobs WHERE id2 >= '$startdate' AND id2 < '$enddate' ORDER BY id"); Working through some beginner stuff and ive copied some code straight out the textbook, word for word yet it produces: Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in C:\wamp\www\PHP Work\StringtoNumbers.php on line 11 <html> <head> <title>Strings Into Numbers</title> </head> <body> <h1>Strings Into Numbers</h1> <ul> <?php $n = "0x237a"; echo "<li>".$n."</li>" echo "<li>".($n * 3)."</li>"; $n2 = "34re5"; echo "<li>".($n2 + 2)."</li>"; $n3 = "boo"; echo "<li>".($n3 + 2)."</li>"; ?> </ul> </body> </html> Hey i'm just woundering how i would echo numbers from a database like 1000000 lets say 1 million how would i go about on echoing it like this 1,000,000 ZhsHero I need to be able to add all the numbers together collected from the database with mysql_fectch_array Code: [Select] public function gradeQuery(){ $id = $this->idQuery(); $sql = "SELECT*FROM grades WHERE moviesID = '$id'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $num_rows = mysql_num_rows($result); } if (preg_match($fname [0-9])) { echo "invalid FirstName"; } want the user to only be able to enter letters this is the code i have assuming that preg_match will spot any number in the $fname field and print "invalid firstname" if the user entered a number. how can i get the users to only insert letters? Hey, I have checkbox and with these I am setting an exam. I want the questions to contain numbers. So for each question a number would be displayed. So far I am Only getting the question and the choices to display. <html> <?php session_start(); include '../Database/connection.php'; $Value22 = mysql_real_escape_string(trim($_POST['myselects'])); $_SESSION['myselect23'] = $Value22; //echo $_SESSION['myselect23']; ?> <body> <form action="Staff_Menu.php" method="post"> <?php include '../Database/previous_q.php'; while($info = mysql_fetch_array( $sql )) { echo "{$info['Que_Question']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice1[]\" value=\"{$info['Que_Choice1']}\" /> "; echo "{$info['Que_Choice1']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice2[]\" value=\"{$info['Que_Choice2']}\" /> "; echo "{$info['Que_Choice2']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice3[]\" value=\"{$info['Que_Choice3']}\" /> "; echo "{$info['Que_Choice3']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice4[]\" value=\"{$info['Que_Choice4']}\" /> "; echo "{$info['Que_Choice4']} <br />\n"; } ?> <input type="submit" value="submit"/> </body> </html> </body> </html> Sorry if this has already been solved. Also, I apologize if this is an amateur question... I have a simple form and a script to do some validating of user data, among other tasks. One part of the script is intended to verify a birthday. Code: [Select] $x = $_POST['x']; $x = trim($x); According to the php.net manual, trim() is supposed to remove whitespace before and after a string. My intent by using trim() is to determine if someone tried to skip the birthday field. I have other validation running on the value of $x, but I wanted trim() to remove whitespace. However, the value of x, which in this case is expected to be numeric, such as 12 for a month, ends up being 1. This happens with any 2-digit number that a user might enter into the form field for x. The second number is always removed. Any insight into why trim() removes the second number, 2 in the above example, leaving only a "1" as the value of $x, would be very greatly appreciated. Thanks in advance. Hello everyone, i am working on a page that displays news topic and would like to have the page numbers at the bottom of the page as links. You know the usual prev (link) then list couple of pages and then next (link). prev - 1 2 3 4 5... next below is the code i have to count my records on my db and display them. but the problem with this is that it displays all of them. page 1 through 50. Code: [Select] $query2 = "SELECT COUNT(title) FROM news"; $result2 = mysql_query($query2); $row = mysql_fetch_row($result2); $total_records=$row[0]; $total_pages = ceil($total_records / 5); for ($i=1; $i<=$total_pages; $i++) { if ($i!=1 and $i !=10) { print "<a href='newsevents.php?page={$i}'> " .$i. " </a>"; } if ($i==$total_pages) { print"<p><a href='newsevents.php?page={$i}'> Last Page </a>"; } if ($i==1) { print "<p class='floatleft'><a href='newsevents.php?page={$i}'> First Page </a>"; } } Can someone help? Hello, Example X = 50 I have been trying to find a way so that I can loop a foreach to insert into a database X and every other number below X unless X already exists. is this possible? -John I have a cards array, and I have this function which will shuffle the cards for me. How can I edit this to 'deal' 12cards (6 & 6) until all the cards are a specific 6 & 6 I pick out? Code: [Select] function ShuffleCards(&$cardsArray, $times) { // Randomizes where to split within center (-3 to +3 from dead center) (MINIMUM 10 CARDS IN DECK) // Splits into two array. Randomizes how many cards from left and how many cards from right (between 1 and 3) // Alternates sides. Continues until both arrays are empty. $arraySize = count($cardsArray); if($arraySize<10) return; $deadCenter = $arraySize/2; for($i=0;$i<$times;$i++) { reset($cardsArray); $cutVariant = rand(-3, 3); $cutLocation = $deadCenter+$cutVariant; $firstArray = array(); $secondArray = array(); for($z=0;$z<$arraySize;$z++) { if($z<$cutLocation) array_push($firstArray, $cardsArray[$z]); else array_push($secondArray, $cardsArray[$z]); } $bottomFirst = rand(0, 1); $cardsArray = array(); while(count($firstArray) && count($secondArray)) { $leftNewCards = array(); $rightNewCards = array(); $leftVariant = rand(1, 3); if($leftVariant>count($firstArray)) $leftVariant = count($firstArray); $rightVariant = rand(1, 3); if($rightVariant>count($secondArray)) $rightVariant = count($secondArray); for($x=0;$x<$leftVariant;$x++) { array_push($leftNewCards, array_shift($firstArray)); } for($y=0;$y<$rightVariant;$y++) { array_push($rightNewCards, array_shift($secondArray)); } if($bottomFirst==0) { $newCardsArray = array_merge($leftNewCards, $rightNewCards); $bottomFirst = 1; } else { $newCardsArray = array_merge($rightNewCards, $leftNewCards); $bottomFirst = 0; } reset($newCardsArray); while(count($newCardsArray)) { array_push($cardsArray, array_shift($newCardsArray)); } } if(count($firstArray)) { while(count($firstArray)) array_push($cardsArray, array_shift($firstArray)); } if(count($secondArray)) { while(count($secondArray)) array_push($cardsArray, array_shift($secondArray)); } } } How I can format the number in a string to be displayed as four-digit. I mean transforming 1 to 0001; and 54 to 0054. hello again how to get ratio with result max 3 numbers such as 3.00 or 0.11 if im do $mynumbers = 1000 / 400; so reusult to be 1.00 ? and how to put full stop if it is more then 4 numbers result instead of 1000 to have 1.000 Hey I have a quick question. I am trying to have a part of this PHP script compare two numbers. There is a randomly generated two-digit number named $random and There is another two digit number named $number. I want to make it so the PHP checks to see if $number has any of the same charcters as $random. I don't know how to do this though. I suspect that somehow both numbers need to be broken apart somehow. Any advice would be appreciated. Let me know if I should explain it more clearly Thanks a ton! Hi ..am new to PHP and am using it to solve some mathematical problems which require extensive computation. Following is a code to solve a problem: Code: [Select] <?php $num =1; $n =0; $temp = 0; $count =0; $Final =0; While( $num < [color=orange]1000000)[/color] { $n = $num; while($n >1) { if(( $n % 2) == 0) { $n = $n/2; $temp++; } else { $n =3*$n +1; $temp++; } } If($temp > $count) { $count =$temp; $Final =$num; } $num++; $temp =0; } echo $Final; echo "<BR>"; echo $count; ?> I am running into "Maximum execution time of 30 seconds exceeded" error...its happening coz of the number 1 million...the code wrks fine for 100,1000,10000 and 100000...but breaks down @ 1000000.....is there any way to solve this situation...or PHP is not capable of handling huge numbers? please help...am stuck with this even though am so close to solving the prob. Thanks Abhijeet This is my current script <?php $iFile = "accounts.txt"; //Put your list lagger's info in this txt file in the same directory as this script. Format is UserID(space)AuthKey $login = file($iFile, FILE_SKIP_EMPTY_LINES); if(!is_file($iFile)) echo "The logins file couldn't be found...".sleep(999999); foreach($login as $line_num => $line) { $login = explode(" ", htmlspecialchars(str_replace(" "," ",$line))); //////////////// ////////////// /////////// ///////// if(stristr($login[1], "\n")) $login[1] = substr($login[1], 0, strlen($login[1])-2); $MobLink = "http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/"; $RefreshStat = file_get_contents($MobLink."refresh_stat?user_id=".$login[1]."&auth_key=".$login[0]); $Mob_Name = explode("<mob_name>", $RefreshStat); $Mob_Name = explode("<", $Mob_Name[1]); $Name = $Mob_Name[0]; $Cash = explode("<cash>", $RefreshStat); $Cash = explode("<", $Cash[1]); $CurrentCash = $Cash[0]; echo $Name."-$".number_format($CurrentCash)." - ".$login[1]."\n"; } sleep(99999); ?> The Script runs through for 100 different accounts, is it possible to add all the Cash values that get echo'd? |