Euler Challenge 349: Javascript: Code works but is too slow
I've been trying to code a solution to project Euler's problem 349 in
javascript (I know this is a less than ideal language for this). The
problem is basically Langtons ant but with 10^18 moves. A full description
of the challenge can be seen here . I managed to get some working code
where I use a array as the grid of squares. If a value in the array is 1
then its black, if its 0, its white. Now the problem with this code is it
is much too slow, it takes about 28 seconds to calculate 1 million moves.
Any ideas on how I might go about optimizing this code?
var grid = [];
for (i = 0; i < 1000000; i++) {
grid.push(0);
}
var sum = 0;
var orientation = 0;
var position = (grid.length / 2);
var rowLength = Math.sqrt(1000000);
var mover = function() {
switch (orientation) {
case -360:
position += 1;
break;
case -270:
position += rowLength;
break;
case -180:
position -= 1;
break;
case -90:
position -= rowLength;
break;
case 0:
position += 1;
break;
case 90:
position += rowLength;
break;
case 180:
position -= 1;
break;
case 270:
position -= rowLength;
break;
case 360:
position += 1;
break;
default:
alert("fault in clockwise switch");
}
};
var check = function() {
for (i = 0; i < grid.length; i++) { //counts all blacks (1's)
if (grid[i]) {
sum += 1;
}
}
};
var movement = function() {
for (i = 0; i < 1000000; i++) { // end condition of i is number of
steps
if (grid[position]) //if it lands on a black
{
grid[position] = 0;
if (orientation === 360) { //keeps orientation below 360
orientation = 0;
}
orientation += 90; //90 degree clockwise turn
mover();
} else if (!grid[position]) { //if it lands on a white
if (!grid[position]) {
if (orientation === -360) {
orientation = 0;
}
grid[position] = 1;
orientation -= 90;
mover();
}
}
}
};
movement();
check();
console.log(position);
console.log(sum);
Sunday, 15 September 2013
UIView inside UIScrollView gets drawn only partially
UIView inside UIScrollView gets drawn only partially
I've searched for a couple of hours without luck so I'll try asking.
I'm using a UIView subclass to represent a timeline control. The timeline
is wide and is contained within a UIScrollView. When it is drawn the first
time everything is fine, but after the view gets hidden (eg. gets behind a
modal view) the timeline view's subsequent calls to drawRect: have always
the same area. The following illustrates the issue:
// Initial call:
(lldb) p rect
(CGRect) $0 = origin=(x=0, y=0) size=(width=896, height=70)
// All the subsequent calls:
(lldb) p rect
(CGRect) $1 = origin=(x=0, y=0) size=(width=320, height=70)
Even calling setNeedsDisplay or setNeedsDisplayInRect: doesn't help. The
view contents need to be drawn only infrequently so the best alternative
would be to draw the control on some kind of persistent canvas.
Any ideas why the content is not drawn properly? My drawRect is currently
applied to the whole control without optimization and still only the
beginning of the
My simplified drawRect: looks like this:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.foregroundColor.CGColor);
// ...some drawing...
// ..and multiple calls to CGContextStrokePath(context), for different
paths
// with different colors. This method ends calling
CGContextStrokePath(context).
}
I've already tried the answers found here, but no luck. Any help is
greatly appreciated.
I've searched for a couple of hours without luck so I'll try asking.
I'm using a UIView subclass to represent a timeline control. The timeline
is wide and is contained within a UIScrollView. When it is drawn the first
time everything is fine, but after the view gets hidden (eg. gets behind a
modal view) the timeline view's subsequent calls to drawRect: have always
the same area. The following illustrates the issue:
// Initial call:
(lldb) p rect
(CGRect) $0 = origin=(x=0, y=0) size=(width=896, height=70)
// All the subsequent calls:
(lldb) p rect
(CGRect) $1 = origin=(x=0, y=0) size=(width=320, height=70)
Even calling setNeedsDisplay or setNeedsDisplayInRect: doesn't help. The
view contents need to be drawn only infrequently so the best alternative
would be to draw the control on some kind of persistent canvas.
Any ideas why the content is not drawn properly? My drawRect is currently
applied to the whole control without optimization and still only the
beginning of the
My simplified drawRect: looks like this:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.foregroundColor.CGColor);
// ...some drawing...
// ..and multiple calls to CGContextStrokePath(context), for different
paths
// with different colors. This method ends calling
CGContextStrokePath(context).
}
I've already tried the answers found here, but no luck. Any help is
greatly appreciated.
showing a hint message upon hovering
showing a hint message upon hovering
i followed a tutorial on how to make a photo gallery using jquery plugins
and html and im trying to make some amendments to it,normally when i click
on an image it zooms out but i want to add a hint such that when i click
on the image it shows a certain hint message for example if i click on an
image it shows a hint this is message number 1,below is the normal code
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Pure CSS3 photo gallery | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<div class="container" id="container">
<div class="gallery">
<a tabindex="1"><img src="images/1.jpg"> </a>
<a tabindex="1"><img src="images/2.jpg"></a>
<span class="close"></span>
</div>
</div>
</body>
</html>
but i want to add something like this
<a tabindex="1"><img src="images/1.jpg"> <p class="hint">image 43 </p></a>
and in the css i add something like this
.gallery:hover .hint{
margin:-30px 0 0 450px;
}
im not very good but i would love to learn this,thanks in advance
i followed a tutorial on how to make a photo gallery using jquery plugins
and html and im trying to make some amendments to it,normally when i click
on an image it zooms out but i want to add a hint such that when i click
on the image it shows a certain hint message for example if i click on an
image it shows a hint this is message number 1,below is the normal code
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<title>Pure CSS3 photo gallery | Script Tutorials</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<div class="container" id="container">
<div class="gallery">
<a tabindex="1"><img src="images/1.jpg"> </a>
<a tabindex="1"><img src="images/2.jpg"></a>
<span class="close"></span>
</div>
</div>
</body>
</html>
but i want to add something like this
<a tabindex="1"><img src="images/1.jpg"> <p class="hint">image 43 </p></a>
and in the css i add something like this
.gallery:hover .hint{
margin:-30px 0 0 450px;
}
im not very good but i would love to learn this,thanks in advance
Updating BCrypt Hash Password of User via JPA
Updating BCrypt Hash Password of User via JPA
I am stucked into a very weird problem. I have not used BCrypt before for
password security. The functionality of the app is to Register a user and
then come with Change Password option. When I register a user and Login in
the app it works fine. But when I update the password I would no more be
able to Login with the new password.Because this function always return
false after updating the password:
BCrypt.checkpw(password.trim(), user.getPasswordHash())
Below the code is given for the save and update function.
*NOTE: I am using Java, GWT, Errai and JPA *
Create User
public void createUser(User user) {
String passwd = user.getPassword();
String salt = BCrypt.gensalt();
user.setPassword(""); // remove the password
user.setPasswordHash(BCrypt.hashpw(passwd, salt));
commonDAO.save(user);
}
Update User (Change Password)
public void updateUser(String pwd) {
String salt = BCrypt.gensalt();
User user2=sessionContext.getCurrentUser();
user2.setPasswordHash(BCrypt.hashpw(pwd, salt));
commonDAO.merge(user2);
}
Following is the login function :
public User login(String username, String password, Boolean rememberMe) {
try {
User user = userDAO.fetchUserByName(username);
System.out.println(user.getId()+":"+user.getUsername());
if (!BCrypt.checkpw(password.trim(), user.getPasswordHash())) {
throw new AuthenticationException("Failure in authentication");
}
return user;
} catch (org.apache.shiro.authc.AuthenticationException e) {
throw new AuthenticationException("Failure in authentication");
// log.error("Failure in authentication", e);
}
}
I am stucked into a very weird problem. I have not used BCrypt before for
password security. The functionality of the app is to Register a user and
then come with Change Password option. When I register a user and Login in
the app it works fine. But when I update the password I would no more be
able to Login with the new password.Because this function always return
false after updating the password:
BCrypt.checkpw(password.trim(), user.getPasswordHash())
Below the code is given for the save and update function.
*NOTE: I am using Java, GWT, Errai and JPA *
Create User
public void createUser(User user) {
String passwd = user.getPassword();
String salt = BCrypt.gensalt();
user.setPassword(""); // remove the password
user.setPasswordHash(BCrypt.hashpw(passwd, salt));
commonDAO.save(user);
}
Update User (Change Password)
public void updateUser(String pwd) {
String salt = BCrypt.gensalt();
User user2=sessionContext.getCurrentUser();
user2.setPasswordHash(BCrypt.hashpw(pwd, salt));
commonDAO.merge(user2);
}
Following is the login function :
public User login(String username, String password, Boolean rememberMe) {
try {
User user = userDAO.fetchUserByName(username);
System.out.println(user.getId()+":"+user.getUsername());
if (!BCrypt.checkpw(password.trim(), user.getPasswordHash())) {
throw new AuthenticationException("Failure in authentication");
}
return user;
} catch (org.apache.shiro.authc.AuthenticationException e) {
throw new AuthenticationException("Failure in authentication");
// log.error("Failure in authentication", e);
}
}
unable to access public ip
unable to access public ip
my server is configured to have a public ip on one network card, and a
private ip on the other network card... the server was running fine after
some time... suddenly i'm unable to access my server from outside my isp
network...
at first i thought it's because i recently changed my ip address, until
the same thing happen to 3 other servers (that i'm in charged to) on
totally different network... one is running with the same isp (same
network as my previous server), one is running with different isp (not
even in the same country), and the other one is running with different isp
(same country)...
the conditions i checked :
from outside the server isp network:
able to ping to the server
traceroute to the server has no problem
unable to access the webserver
unable to access the ssh
unable to access the sftp
from inside the server isp network (using public ip):
able to ping to the server
traceroute to the server has no problem
able to access the webserver
able to access the ssh
able to access the sftp
there's no firewall set on the servers... is there anything i should
check? i'm also asking to my isp as i put this question... this also
happens only on occasion...
my server is configured to have a public ip on one network card, and a
private ip on the other network card... the server was running fine after
some time... suddenly i'm unable to access my server from outside my isp
network...
at first i thought it's because i recently changed my ip address, until
the same thing happen to 3 other servers (that i'm in charged to) on
totally different network... one is running with the same isp (same
network as my previous server), one is running with different isp (not
even in the same country), and the other one is running with different isp
(same country)...
the conditions i checked :
from outside the server isp network:
able to ping to the server
traceroute to the server has no problem
unable to access the webserver
unable to access the ssh
unable to access the sftp
from inside the server isp network (using public ip):
able to ping to the server
traceroute to the server has no problem
able to access the webserver
able to access the ssh
able to access the sftp
there's no firewall set on the servers... is there anything i should
check? i'm also asking to my isp as i put this question... this also
happens only on occasion...
Saturday, 14 September 2013
Using 'Object' functions in Lua?
Using 'Object' functions in Lua?
I want to be able to have 'objects' with certain functions that refer to
themselves (I have no idea what to call this) in Lua. I have seen code of
what i'm trying to do but i have never understood what any of it actually
means. I have tried looking over the Lua website but no luck.
Basic Code:
table = {}
function newTable(...)
...
return setmetatable(table)
end
function table:function(...)
...
end
Can someone explain what is going on here and how i can use this please?
Thanks for reading!
I want to be able to have 'objects' with certain functions that refer to
themselves (I have no idea what to call this) in Lua. I have seen code of
what i'm trying to do but i have never understood what any of it actually
means. I have tried looking over the Lua website but no luck.
Basic Code:
table = {}
function newTable(...)
...
return setmetatable(table)
end
function table:function(...)
...
end
Can someone explain what is going on here and how i can use this please?
Thanks for reading!
To see anonymous function declaration
To see anonymous function declaration
If I have a code:
map foreach { case(k,v) => println("k="+k+";v="+v) }
How may I make sure which kind of anonymous function foreach() function is
accepting ? I mean what kind of declaration was used to define this
function?
I'm guessing it is: Tuple2[Int, String] => Unit
But how may I see it / be sure ? In a console.
If I have a code:
map foreach { case(k,v) => println("k="+k+";v="+v) }
How may I make sure which kind of anonymous function foreach() function is
accepting ? I mean what kind of declaration was used to define this
function?
I'm guessing it is: Tuple2[Int, String] => Unit
But how may I see it / be sure ? In a console.
Subscribe to:
Posts (Atom)