Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:01:16+00:00 2026-06-08T16:01:16+00:00

Earlier I wrote a code in Matlab for this sort of lottery function, just

  • 0

Earlier I wrote a code in Matlab for this sort of lottery function, just to test if it was possible. However, I actually needed it in PHP so I’ve just rewritten the code and it does seem to work, but as it involves a lot of looping I want to make sure I’m doing it as efficiently as possible.

What the code does:

You can call the function $lotto -> type($users,$difficulty) and it will return two numbers. Here’s the explanation, $users is the number of users registered on the website, i.e the people who will potentially buy a ticket. $difficulty is a number between 1 and 10, where 5 is normal, 1 is easy and 10 is hard. Difficulty here means how hard it is to match all numbers on a lottery ticket.

So what are the numbers that the function returns? That would be $n and $r. $n is the amount of numbers there will be on the lottery ticket, and $r is the amount of numbers you can choose from the lottery ticket. For example, in the UK a national lottery ticket has 49 numbers if which you choose 6. I.e $n = 49 and $r = 6.

How does the function calculate these two numbers? In the UK national lottery there are 13,983,816 different possible ticket combinations. If I were to run $lotto -> type(13983816,1) it would return array(49,6). Basically it tried to make it so there are as many combinations of tickets as there are registered users.

tl;dr, here’s the code:

<?php
class lotto {
    public function type($users,$difficulty){
        $current_r = $r = 2;
        $current_n = 0;
        $difficulty = ($difficulty + 5) / 10; // sliding scale from 1 - 10
        $last_tickets_sold = 200; // tickets sold in last lotto
        $last_users = 100; // how many users there were in the last lotto
        $last_factor = $last_tickets_sold / $last_users; // tickets per user
        $factor = $last_factor * $difficulty;
        $users *= $factor;
        while($r <= 10){
            $u = 0;
            $n = $r;
            while($u < $users && $n < 50){
                $u = $this -> nCr(++$n,$r);
            }
            if($r == 2){
                $current_n = $n;
            } elseif(abs($this -> nCr($n,$r) - $users) < abs($this -> nCr($current_n,$current_r) - $users)){
                // this is a better match so update current n and r
                $current_r = $r;
                $current_n = $n;
            }
            $r++;
        }
        return array($current_n,$current_r);
    }
    private function nCr($n,$r){
        return $this -> factorial($n) / (
            $this -> factorial($r) * $this -> factorial($n - $r)
        );
    }
    private function factorial($x){
        $f = $x;
        while(--$x){
            $f *= $x;
        }
        return $f;
    }
}
$lotto = new lotto;
print_r($lotto -> type(1000,5));
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:01:17+00:00Added an answer on June 8, 2026 at 4:01 pm

    I did a quick scan and spotted a few places that can be further optimized.

    Combination
    Your algorithm is a brute force one and can be further optimized

    private function nCr($n,$r){
        return $this -> factorial($n) / (
            $this->factorial($r) * $this->factorial($n - $r)
        );
    }
    

    to

    function nCr($n,$r) {
        $top = 1;
        $sub = 1;
    
        for($i = $r+1; $i <= $n; $i++)
            $top *= $i;
    
        $n -= $r;
        for($i = 2; $i <= $n; $i++)
            $sub *= $i;
    
        return $top / $sub;
    }
    

    Too Much Combination Calculation
    Calculate combination is expensive.

    $u = 0;
    $n = $r;
    while($u < $users && $n < 50){
        $u = $this -> nCr(++$n,$r);
    }
    

    to

    $n = $r + 1;
    $u = nCr($n, $r);
    
    while ($u < $users && $n < 50) {
        $n++;
        $u *= $n;
        $u /= ($n - $r);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I wrote this question up earlier today but I decided to delete it
This question follows an earlier one . Here is some code that reproduces the
Okay this is a revised question from earlier today, I have included code to
This is my code: function func(){ for(i=0; i < 5; i++){ alert('B'); } }
Earlier this week, I had to do something which feels like a semantics violation.
Earlier today I was working on a PHP 5.3+ application, which meant I was
So I was reading about PHP namespaces, and I realized that in versions earlier
| wrote C++/CLI code with .NET framework 4.0.30319, I would like to recompile it
I've wrote a parser in PHP that converts a string representation of an equation
please tell me where is problem. i wrote this xpath query but its not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.