Exforsys

Online Training

Prime or not

This is a discussion on Prime or not within the Java forums, part of the Programming Talk category; This will let you know whether a number is prime or not .. Code: <style> body{text-align:center; ...


Go Back   Exforsys > Programming Talk > Java

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2006, 01:09 PM
Member
 
Join Date: Apr 2006
Posts: 30
insane is on a distinguished road
Prime or not

This will let you know whether a number is prime or not ..
Code:
<style>
body{text-align:center; font-family:arial;}
.answerbox,.detailbox {
       background-color:#FFFFFF; 
       font-size:80;
       height:100;
       color: black;
       font-weight:bold; 
       text-align: center;
       width:950;
       }
       
.detailbox {
       height:80;     
       font-size:50
       }

sup {font-size:40; vertical-align:top; position:relative; top:-5}
      
#quest{
       font-size:20;
       text-align:right;
       border-style: solid;
       border-color: gray;
       border-width: 1;
       width: 130
       }     
</style>
<script>

function power(arr)
  {
  newarr=new Array()
  for(k=0; true; k++)
    {
    base=arr[0]
    for (expo=1; arr[expo]==base;expo++){}
    newarr[k]=base+(expo!=1 ? "<sup>"+expo+"</sup>" : "")
    if (expo>=arr.length) {break}
    arr=arr.slice(expo)  
    }
  return newarr.join("×")
  }

function twodivisors(numb)
  {
  for (var i=2; i<=Math.sqrt(numb); i+=1)
    {
    if (numb%i==0) {return new Array(i,numb/i)}
    }
  return "pr" 
  }

function analyze(num)
  {
  iffalse=detailsplace+"=num+'='+power(primedivisors);"+yesnoplace+"='Number '+num+' is not prime'"
  iftrue=yesnoplace+"='Number '+num+' is prime';"+detailsplace+"=''"
  
  if (num<2) {eval(detailsplace+"='';"+yesnoplace+"='Number '+num+' is not prime'"); return} 
  if (num==2) {eval(iftrue); return}
  
  if (num%2==0)
    {
    primedivisors=new Array()
    primedivisors.push(2)
    for (otherdivisor=num/2; twodivisors(otherdivisor)!="pr"; otherdivisor=twoparts[1])
     {
     twoparts=twodivisors(otherdivisor);
     primedivisors.push(twoparts[0]);
     }
    primedivisors.push(otherdivisor)
    eval(iffalse); return
    }
    
  for (var i=3; i<=Math.sqrt(num); i+=2)
    {
    if (num%i==0) 
     {
     primedivisors=new Array()
     primedivisors.push(i)
     for (otherdivisor=num/i; twodivisors(otherdivisor)!="pr"; otherdivisor=twoparts[1])
      {
      twoparts=twodivisors(otherdivisor);
      primedivisors.push(twoparts[0]);
      }
     primedivisors.push(otherdivisor)
     eval(iffalse); return
     }
    }
  eval(iftrue); return
  }
  
function starter()
  {
  num=document.formh.quest.value
  if (isNaN(num)) {alert("You entered an invalid number."); return}
  detailsplace="document.getElementById('details').innerHTML"
  yesnoplace="document.getElementById('answer').innerHTML"
  analyze(eval(num))
  }  
  
</script>

<div class=answerbox id=answer></div>
<div class=detailbox id=details></div>
<form name=formh>
  Number: <input type=text id=quest><br>
  <input type=button value="Is it prime?" onClick=starter()><br>
</form>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
prospective PM of India India Chit-Chat 2 05-22-2004 10:45 AM


All times are GMT -4. The time now is 07:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.