+15 votes
by (100 points)

I am trying to solve the following (Project Euler) problem using R ( and iterators and the foreach package ),

What is the smallest positive number that is divisible by all of the numbers from one to fifteen?

and while I think that my code should do the job, it does not seem to:

library(foreach)
library(iterators)

# function to check sequence of natural numbers for  
#   divisibility by a given list of factors
fnDivision = function(maxNum, vFactors) {
  foreach(i = icount(factorial(15))) %do% {
    if(!i %% 100 ) cat(sprintf("Done with the first %i natural numbers.\n", i))
    if(all(! i %% vFactors)) { 
      return(i)
    }
  } 
}

# test the function
vFactors = c(8, 9, 10, 11, 12, 13, 14, 15)
fnDivision(15, vFactors)

Note that I have reduced the number of factors by which I test the division of the sequence of natural numbers from all the natural numbers from 1-15, to the ones above.

Just in case, the correct answer to this is given in A003418 , as 360360, and this

all(! 360360 %% vFactors)

evaluates to TRUE .

12 Answers

+19 votes
by (100 points)
help.search("least common multiple") 

library(gmp)
Reduce(lcm.bigz, 1:15)
# Big Integer ('bigz') :
# [1] 360360
+16 votes
by (100 points)

Here you go.

x <- 8:15
p <- prod(x)
min(Reduce(intersect, lapply(x,function(i) seq(i,p,i))))

[1] 360360

And you're probably getting the wrong answer because you're forgetting to include 8.

+13 votes
by (100 points)

Given your set of reduced divisors, this should be very fast (yes, even though it's a for loop - it only has iterations equal to the length of divisors) and relies on multiplying the greatest power of each of the prime factors in your divisors...

#  For primeFactors function
require( surveillance )
x <- c( 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 )
#  Output vector
out <- numeric(0)
#  Maths magic
for( i in x ){
  tmp <- primeFactors(i)
  out <- c( out , tmp[ ! tmp %in% out ] ) 
}
prod( out )
[1] 360360
+11 votes
by (100 points)

So after some thought, I figured that my attempt at using the foreach package to access the iterator stream was misguided. Instead, here is the (highly Pythonic) solution that I am happy with:

library(iterators)

# function to check sequence of natural numbers for  
#   divisibility by a given list of factors
fnDivision = function(maxNum, vFactors) {
    i = icount(factorial(15))
    while(TRUE) {
        currentlyTesting = nextElem(i)
            if(all(! currentlyTesting %% vFactors)) { 
            return(currentlyTesting )
            }
    } 
}

# test the function
vFactors = c(8, 9, 10, 11, 12, 13, 14, 15)
sprintf('The smallest natural number divisible by the first 15 natural numbers is %i.', 
    fnDivision(15, vFactors))
+7 votes
by (100 points)

I currently see two possibilities:

Possibility one: Include details immediately in the Categories data set. This makes it reasonable to keep all of the repeated names because they are discriminated here.

Possibility two: Do not include any details besides the name in Categories, as you currently have it and combine them. If this ID is just acting as a name and you have other factors to sort by (discerning one bathroom project from another by project id for instance) then you should combine them. If you don't, you could run into scaling issues since you are using up so much unnecessary space.

+5 votes
by (100 points)

Please try this:

function SelectAdmin() {
    global $mysqli;//You need this !
    $sql = "SELECT * from admin order by id ASC";
    $result=$mysqli->query($sql);
    return $result; 
}
+3 votes
by (100 points)

The first step would be to have the same result as .load , but with AJAX. First, your url should be your variable, not a string with a PHP variable. Then on complete, you need to find the target :

$.ajax({
    url: post_id, //Your URL should be your variable
    cache: false,
    beforeSend: function (data) {
        $('#single_product_box').html("Loading...");
    },
    success: function (data) {
        if (data!='error'){
            //Have to target you element.
            $('#single_product_box').html($(data).find('#main')); //Here i'm not sure if you should add .html();
        }else{
            $('#single_product_box').html("<span style='color:red;'>Error!</span>");
        }
    },
    error: function (data) {
        $('#single_product_box').html("<span class='error'>Error!</span>");
    }
});

Now that you have the content, you have the same problem about the carrousel. Just retrigger the carousel!

To do that, you'll have to change the dataType to text , because your page is reloading jQuery. When you reload it, it remove the prototype method jCarouselLite and can't access it.

After appending the HTML, you can use the same line of code you are using on the DOM ready!

Here the final code

$.ajax({
    url: post_id, //Your URL should be your variable
    cache: false,
    dataType: 'text', //Passing text here will prevent the script from runnig. Script were overriding jCarousel and couldnt active it again
    beforeSend: function (data) {
        $('#single_product_box').html("Loading...");
    },
    success: function (data) {
        if (data!='error'){
            //Have to target you element.
            var html = $(data).find('#main');
            $('#single_product_box').html(html);
            
            //Retrigger the carousel!
            $(".anyClass-2").jCarouselLite({
                btnNext: ".next-any2",
                btnPrev: ".prev-any2"
                
            });
        }else{
            $('#single_product_box').html("<span style='color:red;'>Error!</span>");
        }
    },
    error: function (data) {
        $('#single_product_box').html("<span class='error'>Error!</span>");
    }
});
0 votes
by (100 points)

Just add a textbox and a button in html:

<input type="text" id="url"/>
<input type="button" value="Create Url" onclick="createUrl()"/>
<p id="output"></p>

And in javascript on clicking of the button, just make the full url and then show it to user:

function createUrl() {
 var base = "https://www.youtube.com/watch?v=";
 document.getElementById("output").innerHTML = "<a href='" + base + document.getElementById("url").value + "'>" + base + document.getElementById("url").value + "</a>";
}

Hope this helps.

0 votes
by (100 points)

you must set CommandArgument and CommandName in linkbutton tag, then use Command event not Click

    <ItemTemplate>
            <b>Test Name:</b> <%# DataBinder.Eval(Container.DataItem, "Name")%> <br />
            <b>Test Phone:</b> <%# DataBinder.Eval(Container.DataItem, "Phone")%> <br />
                <asp:LinkButton ID="btnView" runat="server" Text="View" OnCommand="btnView_Click" CommandName="ShowDetails" CommandArgument='%# DataBinder.Eval(Container.DataItem, "Phone")%' ></asp:LinkButton>
</ItemTemplate>

then handle that argument in codebehind.. CommandArgument in MSDN

0 votes
by (100 points)

If you have .NET Core CLI installed, you can try:

dotnet --list-runtimes

Which will print something like:

Microsoft.NETCore.App 3.0.0 [c:\program files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [c:\program files\dotnet\shared\Microsoft.NETCore.App]

Reference: Microsoft doc

Related questions

+14 votes
5 answers
+6 votes
8 answers
+9 votes
9 answers
...