Prime Number - Data while loading

Posted by Emroot on Stack Overflow See other posts from Stack Overflow or by Emroot
Published on 2010-04-03T02:02:07Z Indexed on 2010/04/03 2:03 UTC
Read the original article Hit count: 312

Filed under:
|

Hi,

I was trying in Ruby on Rails how to find a prime number. Here is my code :

helper : app/helpers/test_helper.rb

module TestHelper                
  def prime_number? number     
    index = 2                    
    tmp = 0                      
    while index <= number        
      if tmp < 1                 
        if (number % index) == 0 
          tmp += 1               
        end                      
      else                       
        return false             
      end                        
      index += 1                 
    end                          
   return true                   
  end                            
end

and my view : app/views/test/index.html.erb

<% (2..100).each do |i| -%>
  <% if prime_number? i %>
    <%= i %>
  <% end -%>
<% end -%>

So my question is : How can you load data while it's calculating ? I mean if I replace 100 by 100000 in my view, how can I see data on my view while my helper method is calculating ? Do I need to use ajax or rails provide a tool for that ?

Thank you.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails