Priemgetallen vinden

Gestart doorRogier13 reacties38 weergaven

14 berichten · pagina 1 van 1

Rogier

Lid · Topicstarter

Berichten
6.485
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Was vandaag weer ff nutteloos bezig dus heb priemgetallen zoekscript gemaakt biggrin
[COLOR= #0000BB]<?php[/COLOR] 
[URL="http://www.php.net/class"]class[/URL] PriemGetal [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$priemgetallen[/COLOR][COLOR= #007700];

    function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$eind[/COLOR][COLOR= #007700]) {[/COLOR]
        [URL="http://www.php.net/for"]for[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]<[/COLOR] [COLOR= #0000BB]$eind[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$a[/COLOR][COLOR= #007700]++) {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]priemgetallen[COLOR= #007700][[/COLOR][COLOR= #0000BB]$a[/COLOR][COLOR= #007700]] = ([/COLOR][COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]==[/COLOR] [COLOR= #0000BB]1[/COLOR] [COLOR= #007700]||[/COLOR] [COLOR= #0000BB]$a[/COLOR] [COLOR= #007700]==[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700]) ?[/COLOR] [COLOR= #0000BB]0[/COLOR] [COLOR= #007700]:[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700];
        }[/COLOR]
    
        [URL="http://www.php.net/for"]for[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$b[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]2[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$b[/COLOR] [COLOR= #007700]*[/COLOR] [COLOR= #0000BB]$b[/COLOR] [COLOR= #007700]<[/COLOR] [COLOR= #0000BB]$eind[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$b[/COLOR][COLOR= #007700]++) {[/COLOR]
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]priemgetallen[COLOR= #007700][[/COLOR][COLOR= #0000BB]$b[/COLOR][COLOR= #007700]] ==[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]) {[/COLOR] 
                [URL="http://www.php.net/for"]for[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$c[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$b[/COLOR] [COLOR= #007700]*[/COLOR] [COLOR= #0000BB]$b[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$c[/COLOR] [COLOR= #007700]<=[/COLOR] [COLOR= #0000BB]$eind[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$c[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$c[/COLOR] [COLOR= #007700]+[/COLOR] [COLOR= #0000BB]$b[/COLOR][COLOR= #007700]) {[/COLOR]
                    [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]priemgetallen[COLOR= #007700][[/COLOR][COLOR= #0000BB]$c[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];
                }
            }
        }
    }
}[/COLOR]

[COLOR= #0000BB]$eind[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]19999[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #0000BB]$class[/COLOR] [COLOR= #007700]= new[/COLOR] Priemgetal[COLOR= #007700]([/COLOR][COLOR= #0000BB]$eind[/COLOR][COLOR= #007700]);[/COLOR]

?>


Met deze code kan je de array priemgetallen aanroepen door $class->priemgetallen te doen.

De niet-priemgetallen staan er voor het gemak nog in. Als je bijv een tabel wil maken. http://zoekleven.nl/priem.php

Succes en heb je nog suggesties mag je me die pmen smile
0
Berichten
1.631
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Leuk zal kijken of ik dit nog kan gebruiken. Btw leuk domein zoekleven.nl ^_^
0
Berichten
238
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Leuk domein haha! smile
0
Berichten
8.649
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Snap er niets van. Hoe gebruik ik hem en wat kan ik ermee?
0
Berichten
5.957
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Je kan er de priemgetallen mee vinden.
Je doet new priemgetal(9999); waar 999 het max nummer is.
en dan kun je de array met getallen aanvragen door $class->priemgetallen te doen.
0
Rogier

Lid · Topicstarter

Berichten
6.485
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
bijvoorbeeld:

[COLOR= #0000BB]<?php
$eind[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]19999[/COLOR][COLOR= #007700];[/COLOR]

[COLOR= #0000BB]$class[/COLOR] [COLOR= #007700]= new[/COLOR] Priemgetal[COLOR= #007700]([/COLOR][COLOR= #0000BB]$eind[/COLOR][COLOR= #007700]);[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<table><tr>[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/for"]for[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$i[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$i[/COLOR] [COLOR= #007700]<=[/COLOR] [COLOR= #0000BB]$eind[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$i[/COLOR][COLOR= #007700]++) {[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/floor"]floor[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$i[/COLOR] / [COLOR= #0000BB]10[/COLOR][COLOR= #007700]) ==[/COLOR] [COLOR= #0000BB]$i[/COLOR] / [COLOR= #0000BB]10[/COLOR][COLOR= #007700]) {[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]</tr><tr>[/COLOR][COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$class[/COLOR][COLOR= #007700]->[/COLOR]priemgetallen[COLOR= #007700][[/COLOR][COLOR= #0000BB]$i[/COLOR][COLOR= #007700]] ==[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]) {[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<td class="true">[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$i[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]</td>[/COLOR][COLOR= #007700];
    }[/COLOR]
    [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]<td class="false">[/COLOR][COLOR= #007700].[/COLOR][COLOR= #0000BB]$i[/COLOR][COLOR= #007700].[/COLOR][COLOR= #DD0000]</td>[/COLOR][COLOR= #007700];
    }        
}[/COLOR]?>
0
Berichten
5.795
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Voor zoiets kleins kun je beter geen class gebruiken maar gewoon 1 apparte functie.
maar voor de rest is hij wel goed
0
Fass

Lid

Berichten
3.771
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Ik kan het maar niet snappen sadley.. Wil je comments plaatsen? biggrin
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
Fass schreef:
Ik kan het maar niet snappen sadley.. Wil je comments plaatsen? biggrin
Het is wel in classes gebouwd, dus als je dat niet snapt raad ik aan om je daarin eens te verdiepen.
0
Fass

Lid

Berichten
3.771
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nee, daar ligt het niet aan, helaas cry
Als ik dit ding volg, kom ik eruit dat alleen 0 en 1 priemgetallen zijn?

//Nevermind. Ik deed FALSE : TRUE in plaats van TRUE : FALSE hypocrite. Domme ik
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Zoals al is gezegd is een class voor één functie in dit geval erg onnodig.

Verder wel een mooie functie. Zelf had ik het op een andere manier gedaan:

[COLOR= #0000BB]<?php[/COLOR] 
    
    [COLOR= #007700]function[/COLOR] Priemgetallen[COLOR= #007700]([/COLOR][COLOR= #0000BB]$parEind[/COLOR][COLOR= #007700]) {[/COLOR]
        [COLOR= #0000BB]$tmpPriem[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]

        [URL="http://www.php.net/for"]for[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$i[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$i[/COLOR] [COLOR= #007700]<=[/COLOR] [COLOR= #0000BB]$parEind[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$i[/COLOR][COLOR= #007700]++)
        {[/COLOR]
            
            [COLOR= #0000BB]$boolPriem[/COLOR] [COLOR= #007700]=[/COLOR] TRUE[COLOR= #007700];[/COLOR]
            [URL="http://www.php.net/for"]for[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$j[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700];[/COLOR] [COLOR= #0000BB]$j[/COLOR] [COLOR= #007700]<[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$tmpPriem[/COLOR][COLOR= #007700]);[/COLOR] [COLOR= #0000BB]$j[/COLOR][COLOR= #007700]++)
            {[/COLOR]
                [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](([/COLOR][COLOR= #0000BB]$i[/COLOR] [COLOR= #007700]%[/COLOR] [COLOR= #0000BB]$tmpPriem[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$j[/COLOR][COLOR= #007700]]) ==[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])
                {[/COLOR]
                    [COLOR= #0000BB]$boolPriem[/COLOR] [COLOR= #007700]=[/COLOR] FALSE[COLOR= #007700];[/COLOR]
                    [URL="http://www.php.net/break"]break[/URL][COLOR= #007700];
                }
            }[/COLOR]
            
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$boolPriem[/COLOR] [COLOR= #007700]===[/COLOR] TRUE[COLOR= #007700])[/COLOR]
                [COLOR= #0000BB]$tmpPriem[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$i[/COLOR][COLOR= #007700];

        }[/COLOR]
        
        [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$tmpPriem[/COLOR][COLOR= #007700];
    }[/COLOR]

[COLOR= #0000BB]$arr[/COLOR] [COLOR= #007700]=[/COLOR] Priemgetallen[COLOR= #007700]([/COLOR][COLOR= #0000BB]99[/COLOR][COLOR= #007700]);[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]FORUMCODE0[/COLOR][COLOR= #007700];[/COLOR]
?>


Hierbij kijk je of het betreffende getal deelbaar is door een van de vorige gevonden priemgetallen. Wanneer dit mogelijk is weet je dus dat het getal geen priemgetal is. Een priemgetal is immers een getal dat enkel deelbaar is en door zichzelf.

Maar daarentegen is jouw functie voor performance misschien net wat beter, aangezien bij mij telkens de count() opgeroepen wordt. Ook werkt mijn functie net wat anders (hij slaat de priemgetallen op in plaats van een boolean op te slaan van of het getal een priemgetal is).
0
Rogier

Lid · Topicstarter

Berichten
6.485
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Deze functie staat in een class en was te lui om hem er helemaal uit te slopen.

@Rik: Die manier werkt ook zie ik, alleen denk ik dat mijn functie meer aankan omdat die van mij minder vaak de 2 for gebruikt. Ik maak gebruik van de zeef van blablanogwat.
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
De zeef van Eratosthenes yummie
En ik zeg al. Voor performance is die van jou misschien beter.
0
Berichten
9.953
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Handig, ben slecht in wiskunde dus als ik het nodig heb ga ik hem zeker gebruiken. emo
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren