wikedx Posted November 26, 2014 Report Posted November 26, 2014 (edited) Salut am o problema cu afisarea numerelor prime din vectorul<?phperror_reporting(E_ALL ^ E_NOTICE);$ok=0;$k=0;$d=2;$i=1;$vectorS = array(31,33,434,2);for( $i; $i <= 4; $i++ ){ $ok=1; for( $d; $d <= $vectorS[i]/2; $d++ ) { if($vectorS[i]%$d==0) $ok=0; if($ok==1) $k++; }} if($k>0) print $k; else print 'Nu exista';?>Imi afiseaza "Nu exista" in loc de "2"Executare cod online: http://bit.ly/11UMqnfApasati pe butonul Execute Code Edited November 26, 2014 by wikedx Quote
Ganav Posted November 26, 2014 Report Posted November 26, 2014 Initializeaza-l pe $d cu 2 in for:for( $d = 2; $d <= $vectorS[i]/2; $d++ ){ if($vectorS[i]%$d==0) $ok=0; if($ok==1) $k++; } Quote
wikedx Posted November 26, 2014 Author Report Posted November 26, 2014 d l-am initializat la inceput cu 2 si varianta lui siur nu merge Quote
Ganav Posted November 26, 2014 Report Posted November 26, 2014 (edited) Salut am o problema cu afisarea numerelor prime din vectorul<?phperror_reporting(E_ALL ^ E_NOTICE);$ok=0;$k=0;$d=2;$i=1;$vectorS = array(31,33,434,2);for( $i; $i <= 4; $i++ ){ $ok=1; for( $d; $d <= $vectorS[$i]/2; $d++ ) { if($vectorS[$i]%$d==0) $ok=0; if($ok==1) $k++; }} if($k>0) print $k; else print 'Nu exista';?>Imi afiseaza "Nu exista" in loc de "2"Executare cod online: PHP Sandbox, test PHP online, PHP testerApasati pe butonul Execute Code<?phperror_reporting(E_ALL ^ E_NOTICE);$ok=0;$k=0;$d=2;$i=1;$vectorS = array(31,33,434,2);for($i = 0; $i < 4; $i++ ){ $ok=1; for( $d = 2; $d <= $vectorS[i]/2; $d++ ) { if($vectorS[i]%$d==0) $ok=0; } if($ok==1) $k++;} if($k>0) print $k; else print 'Nu exista';?> Edited November 26, 2014 by Ganav Quote
jsonwhite Posted November 26, 2014 Report Posted November 26, 2014 (edited) $vectorS[$i], nu $vectorS$ok=0;$k=0;$d=2;$i=1;$vectorS = array(31,33,434,2);for($i = 0; $i < 4; $i++ ){ $ok=1; for( $d = 2; $d <= $vectorS[$i]/2; $d++ ) if($vectorS[$i]%$d==0) $ok=0; if($ok==1) $k++;} if($k>0) print $k; else print 'Nu exista'; Edited November 26, 2014 by jsonwhite Quote