Prime Or Not.

Apr 8, 2006

I'm not too experienced in programming, all I know is from practicing so don't be surprised if I made some mistakes. One mistake that I know is that the page is absolutely non-standard, but I don't really care about things like web-standards when making these little scripts.
As I'm Hungarian and not English, this code (functions, vars) was also written in Hungarian, but I translated it to be a bit more readable for Englishpeople. If it's still not clear, I can comment it.

<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>

View 2 Replies


ADVERTISEMENT

Writing Prime Numbers To Table

Jan 26, 2011

I can't seem to get past my function writing
<table rows='10 cols='10' border='1'><tr><th>Prime Numbers(1-100)</th></tr>
<!DOCTYPE HTML PUBLIC "-W3CDTD HTML 4.01EN" "[URL]">
<html>
<head>
<script type="text/javascript">
function primeNumbers() {
var tableCell = 0;
document.write("<table cols='10' border='1'>");
document.write("<tr>"); .....

View 4 Replies View Related

Displaying List Of Prime Numbers Only

Jun 23, 2011

So I wrote this piece of code, which should display the first 10,001 prime numbers.
Code:
<html><head>
<title>Problem 7 project euler</title>
</head><body><script>
var x = 3;
var primes = new Array();
primes[1] = 2;
var n = 1;
document.write("PRIME#1= 2" + "<br />")
while (n < 10001){
if (x%primes[n] == 0){
x = x + 2;
}else if ((n + 1) < (primes.length)){
n = n + 1
}else{
primes[(n+1)] = x;
document.write("PRIME#" + (n + 1) + "= " + primes[(n+1)] + "<br />");
x = x + 2;
n = 1
}}
</script>
</body>
</html>
Now the problem is that somehow this code also interprets some non-primes as primes.

View 7 Replies View Related

Displaying First 1000 Prime Numbers In Table

Jan 27, 2009

I am taking a javascript class and I need a script that displays the first 1000 prime numbers in a table with 10 numbers per row. I've been trying on my own for about 4 hours now and am really confused. I just started the class and have only started learning javascript 3 days ago.

View 6 Replies View Related

"speed Up" Prime # Test Script With These Modifi?

Nov 15, 2011

I have this code for figuring out whether a given number is prime or not:

var SMALLEST = 2;
var BIGGEST = 9E15;
var n = prompt("Enter a number and I'll check if it is prime");
if (isNaN(n) || n < SMALLEST || n > BIGGEST || n % 1 !== 0) {
alert("I can only test integers between " + SMALLEST + " and " + BIGGEST);

[Code]...

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved